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::InitialThreadA ndProcess( )
{
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?
...
...
BOOL CMyService::InitialThreadA
{
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?
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_sta rter,(void *)this,0,& dwThreadId );
unsigned long _stdcall my_starter(void* ptr)
{
CMyService *s = (CMyService*)ptr;
ptr->ThreadFunc();
}
then to start a thread
CreateThread(NULL,0,my_sta
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::InitialThreadA ndProcess( )
{
..
CreateThread(NULL,0,my_sta rter,(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.
// 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::InitialThreadA
{
..
CreateThread(NULL,0,my_sta
..
}
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
#include.....
new function
class functions
ASKER
Yes, I did so.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
LPVOID lpParam;
unsigned long _stdcall my_starter(void * ptr)
{
CMyService *s = (CMyService*)ptr;
s->ThreadFunc(lpParam);
return 0;
}
Anyway, you get the points.
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