Solved

Creating a Thread Function !!!

Posted on 2002-03-30
11
489 Views
Last Modified: 2007-12-19
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
Comment
Question by:Jono
  • 4
  • 4
  • 3
11 Comments
 
LVL 1

Expert Comment

by:lidorc
ID: 6907708
How did you define LPTHREAD_START_ROUTINE and MessageThread?
Also send me the prototype of CreateThread() from the MSDN
0
 
LVL 4

Expert Comment

by:mblat
ID: 6907789
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
 

Author Comment

by:Jono
ID: 6907798
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Jono
ID: 6907860
sorry mblat already tried it and it doesn't work
the same error message is given !!!!as asked in the question...
0
 
LVL 4

Accepted Solution

by:
mblat earned 65 total points
ID: 6907867
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
 
LVL 1

Expert Comment

by:lidorc
ID: 6907923
mblat is right..
but don't forget the __stdcall
0
 
LVL 4

Expert Comment

by:mblat
ID: 6907944
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
 
LVL 1

Expert Comment

by:lidorc
ID: 6907962
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
 
LVL 4

Expert Comment

by:mblat
ID: 6907981
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
 

Author Comment

by:Jono
ID: 6908581
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
 

Author Comment

by:Jono
ID: 6908583
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question