• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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 >*<
0
Jono
Asked:
Jono
  • 4
  • 4
  • 3
1 Solution
 
lidorcCommented:
How did you define LPTHREAD_START_ROUTINE and MessageThread?
Also send me the prototype of CreateThread() from the MSDN
0
 
mblatCommented:
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
0
 
JonoAuthor Commented:
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!!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JonoAuthor Commented:
sorry mblat already tried it and it doesn't work
the same error message is given !!!!as asked in the question...
0
 
mblatCommented:
Ok here is the deal ( or at least I think it is whta the problem is)

you are defining
Message Thread as a member of class.  You can't do that.  It is callback function and to define it is as a member of class you have to declare it as static.

Either declare it as static or remove it from class declaration and make it just global function.

Hope it helps.
0
 
lidorcCommented:
mblat is right..
but don't forget the __stdcall
0
 
mblatCommented:
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. :-(
0
 
lidorcCommented:
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 :)
0
 
mblatCommented:
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... :-)
0
 
JonoAuthor Commented:
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
0
 
JonoAuthor Commented:
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
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now