Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

Problem by CreateThread() ...

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?

Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of chenwei
chenwei

ASKER

I am a little bit confused. Could you be so kind telling me where can I set your code in my program?
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);
Avatar of chenwei

ASKER

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.
the new function must go after the header(s) that declares the class

#include.....

new function

class functions
Avatar of chenwei

ASKER

Yes, I did so.
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chenwei

ASKER

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.