Advertisement

C++ class and function pointer ?

Started by May 25, 2001 06:58 PM
1 comment, last by Kapis 23 years, 8 months ago
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.
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.
Advertisement
Thanks, with a little modification it is compiling fine. Now I can try to get some real work done.

This topic is closed to new replies.

Advertisement