We help IT Professionals succeed at work.

Problem by CreateThread() ...

chenwei
chenwei asked
on
I am going to write a Multi-Thread program in C++. In my program it looks like:

...
...
BOOL CMyService::InitialThreadAndProcess()
{
   DWORD dwThreadId, dwThrdParam = 1;
    HANDLE hThread;
    hThread = CreateThread(
        NULL,                        // no security attributes
        0,                           // use default stack size  
       CMyService::ThreadFunc,      // thread function
        &dwThrdParam,                // argument to thread function
        0,                           // use default creation flags
        &dwThreadId);                // returns the thread identifier
 
   if (hThread == NULL)
      printf("ok");

   CloseHandle( hThread );
     return TRUE;
}

DWORD WINAPI CMyService::ThreadFunc( LPVOID lpParam )
{
   MessageBox( NULL, "Test", "Thread created.", MB_OK );

    return 0;
}

...
...

And the H-file loks like:
...
...
class CMyService
{
...
...
public:
   virtual BOOL      InitialThreadAndProcess();
   DWORD WINAPI ThreadFunc( LPVOID lpParam ) ;
...
...
};

But as I want to compile the program, it shows an error message:
'CreateThread' : Konvertion of Parameters 3 from 'unsigned long (void*)' to  'unsigned long (_stdcall*)(void*) 'Typ2' impossible.

But if I don't use C++ Class but simply in C, i.e. I put the functions all in one file, then there is no problem. Why?

Comment
Watch Question

BRONZE EXPERT

Commented:
because the create thread call expects a certain call type, class members are called with thiscall

this is an easy way round

unsigned long _stdcall my_starter(void* ptr)
{
  MyService *s = (MyService*)ptr;
  ptr->StartFunction();
}

now call create thread passinf this function as the address and pass the object casted to void* in the paramater argument

Author

Commented:
I am a little bit confused. Could you be so kind telling me where can I set your code in my program?
BRONZE EXPERT

Commented:
right, at the top of the file that has the source in for the class in question copy this

unsigned long _stdcall my_starter(void* ptr)
{
 CMyService *s = (CMyService*)ptr;
 ptr->ThreadFunc();
}

then to start a thread

CreateThread(NULL,0,my_starter,(void*)this,0,&dwThreadId);

Author

Commented:
Thanks. But i dosen't work. I did what you told me as follow:

// copy the function my_starter at the top of the file
unsigned long _stdcall my_starter(void* ptr)
{
   CMyService *s = (CMyService*)ptr;
   ptr->ThreadFunc(); }
...
...
BOOL CMyService::InitialThreadAndProcess()
{
..
CreateThread(NULL,0,my_starter,(void*)this,0,&dwThreadId);
..
}

DWORD WINAPI CMyService::ThreadFunc( LPVOID lpParam )
{
...
...
}

But it shows another error message:
C2227: The left part of '->ThreadFunc' muss point to a class/structure/union.
BRONZE EXPERT

Commented:
the new function must go after the header(s) that declares the class

#include.....

new function

class functions

Author

Commented:
Yes, I did so.
BRONZE EXPERT
Commented:

  CMyService *s = (CMyService*)ptr;
  ptr->ThreadFunc();

sorry, ptr-> should be s->

whoops

Author

Commented:
Thanks. But I have to complete some thing, like:

LPVOID lpParam;

unsigned long _stdcall my_starter(void * ptr)
{
     CMyService *s = (CMyService*)ptr;
     s->ThreadFunc(lpParam);
     return 0;
}

Anyway, you get the points.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.