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.