Click to See Complete Forum and Search --> : Question with lpStartAdress of CreateThread


eckmul
December 15th, 2004, 05:04 AM
Hi,

First of all, sorry for my english.

I'm making some modifications in a soft that do not use MCF, and I want to use the CreateThread function.

HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);

One of the parameters is the adress of a function, but I would prefer to put a method's adress, and it doesn't compile...

I could create a function which could call the method of my class ( and I must create a global variable with the instance of my class) but that's not very smart.

I'm wondering too if I could cast the adress of my method into the right type, I'm not sure it's very safe. Does anyone have already tried ?

Thanks for your help.

NoHero
December 15th, 2004, 07:16 AM
Since every clas call is a "thiscall" this won't work. The only good solution for this is using a static thread function for this and pass the this pointer to it:


class Thread
{
private:

HANDLE hThread = NULL;
DWORD dwID = 0;

protected:

static DWORD ThreadProc ( LPVOID lpvParam )
{
Thread* thrd = (Thread*)lpvParam;
// Do something here
//
return 0;
}

public:

Thread () {}
~Thread() {}

void Start ( void )
{
hThread = CreateThread(NULL, 0x100, (LPTHREAD_START_ROUTINE)Thread::ThreadProc, (LPVOID)this, 0, &dwID);
// Error checking and so on
}
}


If you need this lpvParam parameter to pass information to the thread, you can store it into your class

Andreas Masur
December 15th, 2004, 09:20 AM
Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312452)....

eckmul
December 15th, 2004, 09:43 AM
Well, I often forget to use the VOID parameter .... I'm sure it will help me to remember in future. ;)

Thanks a lot both of you