Solved

Creating a Thread Function !!!

Posted on 2002-03-30
11
481 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Jono
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

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

Expert Comment

by:mblat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now