[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Creating a Thread Function !!!

Posted on 2002-03-30
11
Medium Priority
?
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 195 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

650 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