🔒 Locked
CreateThread issues....
Advertisement
Original Post
I''m having trouble with creating threads in one of my classes..
I have a func wich I defined as all of the following in the private section of the class defention (I alsp tried the public section):
DWORD WINAMP WorkerThread (void *Sig);
unsigned long __stdcall WorkerThread (void *Sig);
int __stdcall WorkerThread (void *Sig);
but when I go to compile I get the error:
error C2440: ''type cast'' : cannot convert from '''' to ''unsigned long (__stdcall *)(void *)''
None of the functions with this name in scope match the target type
The thing that baffles me is that I cut + Pasted the calling code directly from another app i had written, where it works perfectly...
I tried making it not part of the class and it still doesn''t compile.... I dunno....
-Zims
class methods will have type "__thiscall". you can try a static method (not sure, tho). otherwise you will have to use a function and public class members.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
class SomeThreadedClass
{
public:
static unsigned long __stdcall ThreadRouter(void *pObject);
void ThreadMethod(void);
};
....
CreateThread(..., &ThreadRouter, (void *)this, ...);
...
unsigned long _stdcall SomeThreadedClass::ThreadRouter(void *pObject)
{
SomeThreadedClass *ptr = (SomeThreadedClass *)pObject;
pObject->ThreadMethod();
}
Enjoy!
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
quote:
Original post by Zimans
Why is it that only static function members of a class work for the CreateThread function?
Because they employ the same calling convention as plain (global) functions. You see that _stdcall on the static method? It''s unnecessary; static methods are always __stdcall. Normal methods, however, are __thiscall which is incompatible with the parameter type for CreateThread.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
I thought static methods defaulted to the default calling convention - which typically is __cdecl (though you could set it to __stdcall)? In any case, anywhere the calling convetion matters, it''s a good idea to explicitly state it (like callbacks and dll exports).
(non-static) Methods use the __thiscall convention, which automatically passes a pointer to the object it''s invoked on in ecx. When you use callbacks, it''s very important that the calling conventions match, so that the code calling you back calls your callback correctly (the machine will likely crash if it doesn''t).
I made a template class (CThread<>
that lets me create threads out of member functions, I use it fairly often...
(non-static) Methods use the __thiscall convention, which automatically passes a pointer to the object it''s invoked on in ecx. When you use callbacks, it''s very important that the calling conventions match, so that the code calling you back calls your callback correctly (the machine will likely crash if it doesn''t).
I made a template class (CThread<>
quote:
Original post by Magmai Kai Holmlor
I thought static methods defaulted to the default calling convention - which typically is __cdecl (though you could set it to __stdcall)? In any case, anywhere the calling convetion matters, it''s a good idea to explicitly state it (like callbacks and dll exports).
You''re right, I''m wrong.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement