Link to home
Start Free TrialLog in
Avatar of Jono
Jono

asked on

Creating a Thread Function !!!

i am trying to write a thread which calls a function..

the problem is that i get an error message  from the following line of code...

hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE )MessageThread,0,0,&ThreadId);

the error message is
'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)'

where MessageThread is the function call. i have tired every type of casting, no luck!!!

leaving out the type cast gives this warning
unsigned int (void *)' to 'unsigned long (__stdcall *)(void *)

the application is an MFC and the thread is being called inside a class..

should i really being using CWndThread or am i missing something...
any advise is appreciated >*<
Avatar of lidorc
lidorc

How did you define LPTHREAD_START_ROUTINE and MessageThread?
Also send me the prototype of CreateThread() from the MSDN
it looks that you not calling it properly
it should be

LPVOID lpParameter;
DWORD dwThreadID;

 HANDLE hCommWatchThread = CreateThread(
                 NULL,
                0,
                (LPTHREAD_START_ROUTINE) CommWatchProc,
                lpParameter,
                0,
                &dwThreadID
                );

and CommWatchProc defined as

DWORD FAR PASCAL CommWatchProc( LPVOID lpVoid )
{
// and lpVoid would be lpParameter you secify in a call
}

see
Avatar of Jono

ASKER

hey lidorc

LPTHREAD_START_ROUTINE ==>
Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread.

(MSDN example of CreateThread)
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId);

MessageThread is declared as

UINT MessageThread(LPVOID pParam);

then the function itself is

UINT CIdiGen::MessageThread(LPVOID pParam)

CIdiGen is my class name...

i had tried passing unsigned long * lpdwParam as the parameter but same error message
anything else just post it thx!!
Avatar of Jono

ASKER

sorry mblat already tried it and it doesn't work
the same error message is given !!!!as asked in the question...
ASKER CERTIFIED SOLUTION
Avatar of mblat
mblat

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
mblat is right..
but don't forget the __stdcall
to: lidorc

<i>mblat is right..
but don't forget the __stdcall </i>

thanks.

I didn't understand your comment about __stdcall.
That is what put me on a track thinking that he is trying to use callback as member of a class.  Callbacks use different calling convention from member functions.

So that is what compiler basically complaining about - that we are trying to use function with different calling convention....
Considering that most of the people run into this problem at one time or the other, one would think that Microsoft could make this message more descriptive, really. :-(
to mblat,
well, I'm not sure I understood you correctly, so correct me if I'm wrong..
We're speaking of 2 calling conventions: stdcall and cdecl.
the different is in "who takes the parameters out from the stack". C/C++ works in cdecl (after every call to the function the function that called it has to remover the parameters), which alows passing unknown number of parameters to a function like: printf(char *s, ...). but functions of windows are stdcall because it makes the exe smaller (the function that was called remover the parameters - one code for all calls).
So Microsoft aren't that bad after all, are they? - sorry if I have the problem of not hating Microsoft enough, like a programer should :)
to: lidorc

if are absolutely right.  What I was complaining about is that I had problem, trying to make callback function member of class, Jono has this problem and I am sure 98% of people at one time or other were buffled with this message....

I just wish for such "standard" error MS would provide better descriptions....

By the way I don't hate MS all that much either... :-)
Avatar of Jono

ASKER

Hey mblat u are correct,
i had a feeling that i couldn't use it as a member function... but as u so rightly put it some of the error messages are misleading...
so thanks very much and i award u the brownie points
Avatar of Jono

ASKER

Hey mblat u are correct,
i had a feeling that i couldn't use it as a member function... but as u so rightly put it some of the
error messages are misleading...
so thanks very much and i award u the brownie points