Hi all,
Can any one tell me why this following code is not working.

#include <Windows.h>
#include <iostream>
#include <string.h>

using namespace std;

//Just a function
DWORD WINAPI StartThread1(LPVOID iValue)
{
	int iStart = 0;
	for(int i=iStart;i<=10000;i++)
		cout<<"i = "<<i<<endl;
	return 0;
}

//My Class
class ThreadTest
{
public:
	ThreadTest();
	DWORD WINAPI StartThread(LPVOID iValue = "0");
	void MainFunction();
};

ThreadTest::ThreadTest()
{
}

DWORD WINAPI ThreadTest::StartThread(LPVOID iValue)
{
	char lszParam[3];
	sprintf(lszParam,"%s",(char *)iValue);
	int iStart = atoi(lszParam);
	for(int i=iStart;i<=iStart+10000;i++)
		cout<<"i = "<<i<<endl;
	return 0;
}

void ThreadTest::MainFunction()
{
	HANDLE hThread;
	DWORD dwGenericThread;
	char lszThreadParam[3];
    strcpy(lszThreadParam,"0");
    hThread = CreateThread(NULL,0,StartThread,&lszThreadParam,0,&dwGenericThread);	//Gives error
    //hThread = CreateThread(NULL,0,StartThread1,&lszThreadParam,0,&dwGenericThread);	//works
    if(hThread == NULL)	{
		DWORD dwError = GetLastError();
        cout<<"SCM:Error in Creating thread"<<dwError<<endl ;
        return;
	}
	WaitForSingleObject(hThread,INFINITE);
	TerminateThread(hThread, 0);
}

//Program execution starts here
void main()
{
	ThreadTest thread;
	thread.MainFunction();
}

I am really frustrated with this. can any one tell me where i am making mistake.
Thank you.

Its not working because thread functions must be either static alss members or functions outside any class. Address of a non-static class method can not be passed to any win32 api function. Make StartThread() static and the compiler will probably take it.

Edited 7 Years Ago by Ancient Dragon: n/a

Thank you very much, that solved that problem. actually my requirement is i should be able to call another class function inside the StartThread function, when i make this as a static it is not possible can you advise me any other way of doing this? the following code explains my requirement

#include <Windows.h>
#include <iostream>
#include <string.h>

using namespace std;
//Class contains the function that needs to be accesed
class NeedstoBAccessed
{
public:
	NeedstoBAccessed();
	void printValue(int x);   //function need to accessed
};

NeedstoBAccessed::NeedstoBAccessed()
{
}

void NeedstoBAccessed::printValue(int x)
{	
	for(int i=x;i<=10000;i++)
		cout<<"i = "<<i<<endl;
}
//My Class
class ThreadTest
{
public:
	NeedstoBAccessed objNeedstoBAccessed;
	ThreadTest();
	static DWORD WINAPI StartThread(LPVOID iValue = "0");
	void MainFunction();
};

ThreadTest::ThreadTest()
{
}

DWORD WINAPI ThreadTest::StartThread(LPVOID iValue)
{
	objNeedstoBAccessed.printValue(10);
	return 0;
}

void ThreadTest::MainFunction()
{
	HANDLE hThread;
	DWORD dwGenericThread;
	char lszThreadParam[3];
    strcpy(lszThreadParam,"0");
    hThread = CreateThread(NULL,0,StartThread,&lszThreadParam,0,&dwGenericThread);
    if(hThread == NULL)	{
		DWORD dwError = GetLastError();
        cout<<"SCM:Error in Creating thread"<<dwError<<endl ;
        return;
	}
	WaitForSingleObject(hThread,INFINITE);
	TerminateThread(hThread, 0);
}

//Program execution starts here
void main()
{
	ThreadTest thread;
	thread.MainFunction();
}

Edited 7 Years Ago by play_c: n/a

This article has been dead for over six months. Start a new discussion instead.