C++ class and function pointer ?
I have an input class which is going to be used for DirectInput. I have a function in this class that I want to use in a seperate thread.
The problem is that in the call to CreateThread, the compiler (VC++ 6) says it can''t convert my function pointer.
ie:
DWORD CInput::MouseThread(void *param)
{
//code
}
//inside my init func CInput::Init
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MouseThread, ...);
Error: It says it can''t convert the third parameter to to ''unsigned long (__stdcall *)(void *)''
Any help would be appreciated immensely.
May 25, 2001 07:09 PM
That''s because class member functions have an addition this pointer passed in the ecx register on MSVC. The Win32 API doesn''t know anything about this, and can''t properly call the function from within the new thread..
I usually create a function like this:
DWORD __stdcall MouseThreadWrapper(void* p)
{
CInput* pInput = reinterpret_cast(p);
return p->MouseThread();
}
If MouseThread() is public, this works as is. Otherwise, MouseThreadWrapper() should be declared as a friend in CInput.
BTW, avoid using CreateThread. It doesn''t initialize the C Run-Time correctly. __beginthreadex in the C library does the same thing, and properly initializes the C Run-Time.
I usually create a function like this:
DWORD __stdcall MouseThreadWrapper(void* p)
{
CInput* pInput = reinterpret_cast
return p->MouseThread();
}
If MouseThread() is public, this works as is. Otherwise, MouseThreadWrapper() should be declared as a friend in CInput.
BTW, avoid using CreateThread. It doesn''t initialize the C Run-Time correctly. __beginthreadex in the C library does the same thing, and properly initializes the C Run-Time.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement