Solved

Problem with LPTHREAD_START_ROUTINE

Posted on 2002-05-28
17
2,081 Views
Last Modified: 2012-06-27
Hi,

I'm trying to call some procedure in separate thread. I'm using CreateThread function. However, I get an error message during compile:

error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall*)(void *)'
Thread creation code looks like this:

CreateThread(
     NULL,
     0,
     (LPTHREAD_START_ROUTINE)My_Procedure_Name,
     NULL,
     0,
     NULL);

Procedure declaration looks like this:

DWORD WINAPI My_Procedure_Name(LPVOID lpParam)

I've tried different declarations ('void', 'long WINAPI', even 'LPTHREAD_START_ROUTINE' :oP) It doesn't help.

I presume type cast (LPTHREAD_START_ROUTINE)... doesn't work somehow. Question is "why?" :o(

Thanx.
0
Comment
Question by:Rimvis
[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
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7038597
That works perfectly when I try it.  I placed this:

DWORD WINAPI My_Procedure_Name(LPVOID lpParam)
{
    MessageBeep(MB_ICONASTERISK );
    return(1);
}


*above* the call to CreateThread()

-- Dan

 
0
 
LVL 19

Author Comment

by:Rimvis
ID: 7038638
Dan:
What do you mean by "*above* the call to CreateThread()"?

Function 'My_Procedure_Name' must be above function that creates thread IN SOURCE CODE? How this can help?

But it DOESN'T :o(
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7038674
>>How this can help?

By declaring the function in a line above the code that uses it (that is, above the CreateThread call), the compiler knows that it is a DWORD-returning function that takes one void* parameter.

If you have done that and still get a compiler error, please copy the exact text of the error message (all of it) here and I'll see if I can figure out what is going on.

-- Dan
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:Rimvis
ID: 7038687
Compiling...
Tester2.cpp
C:\Rimvis\RubiSafe\Services\Test\DCOM\ServiceS\DCOMSSlave\Tester2.cpp(96) : error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)'
        None of the functions with this name in scope match the target type
Error executing cl.exe.
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7038956
The '' suggests your function declaration is up the spout. It means the compiler doesn't know anything about My_Procedure_Name.

So in that sense it looks like Dan is right...

You can just put a function prototype at the start of your program:


DWORD WINAPI My_Procedure_Name(LPVOID lpParam);

...

CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)My_Procedure_Name, NULL, 0, NULL);

...

DWORD WINAPI My_Procedure_Name(LPVOID lpParam)
{
...
}

0
 
LVL 4

Accepted Solution

by:
mblat earned 50 total points
ID: 7039596
Lemme guess: your declaring you thread function as member of a class?
If this is the case you can't do that.  Either declare it outside of the class (as  mirtol showed) or use static keyword.


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7039741
He can't be declaring the thread proc as a class member.  It is impossible!  If RimVis were trying to use a class member, I'm sure he would have posted his sample code like this:

DWORD WINAPI MyObject::My_Procedure_Name(LPVOID lpParam)
{
...
}

About the only possibility left is that DWORD is not the same as an unsigned long.  I think you need to really screw up to have that be the case, but maybe its worth a try... Try this:

unsigned long WINAPI My_Procedure_Name(LPVOID lpParam)
{
   MessageBeep(MB_ICONASTERISK );
   return(1);
}
...
CreateThread( NULL, 0, My_Procedure_Name, NULL,0, NULL );

-- Dan
0
 
LVL 4

Expert Comment

by:mblat
ID: 7039798
2 DanRollins..

I looked through this thread and don't see where Asker shows how and where he declares his thread control proc.  What makes me think that he may declare it inside a class is the error message that is typical of this situation and that this question is farily often asked....
I am new on this board, but I remember it was asked like 2 times in a last 3 month....

For example:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20283272

And he had a problem declaring it inside the class...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7039957
Yes mblat, you are most likely correct.  I am always amazed at the incomplete and inaccurate info we so often get.

Rimvis,
If you are trying to use a C++ class object member function as the threadproc, one way to do that is to declare that member function as static:

//------------------------ in MyObject.h
class CMyObject {
public:
     static DWORD WINAPI My_Procedure_Name(LPVOID lpParam);
     int m_n; // other member fns here...
};

//------------------------ in MyObject.cpp

unsigned long WINAPI CMyObject::My_Procedure_Name(LPVOID lpParam)
{
  MessageBeep(MB_ICONASTERISK );
  return(1);
}

//--------------------------- when you want to start the thread
...
::CreateThread( NULL, 0, CMyObject::My_Procedure_Name, NULL,0, NULL );

-- Dan
0
 
LVL 19

Author Comment

by:Rimvis
ID: 7041038
Yes, it WAS class member :o)

Sorry I hadn't mentioned it, didn't thought that coyld be the case. Sorry :o/

Thanx to all.
0
 
LVL 4

Expert Comment

by:mblat
ID: 7041711
Answer grade B? Can I ask why?
0
 
LVL 19

Author Comment

by:Rimvis
ID: 7041764
Well... B means 'Good', isn't it?
Gues my question wasn't that complicated.
You said: "I remember it was asked like 2 times in a last 3 month".
I've chosen your comment as an answer, because you mentioned why I was having problems EXACTLY. However, other people also mentioned parts of my solution.
I couldn't give point to all of them, and to assign all merits to you was ... well... unfair.

So I gave B. Sorry if I offended you. And again, thanx for your answer ;o)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7042351
Since you couldn't give points to everybody, you take away points from mblat.  It makes perfect sense...

-- Dan
0
 
LVL 4

Expert Comment

by:mblat
ID: 7042475
Not to mention you COULD gibe points to everybody...  And no, B doesn't mean GOOD at this forum. :-(
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7042541
mblat,
On the other hand, yours was not a paragon of perfection, answerwise.  For instance, you did not suggest how to solve the problem... you only suggested a possible cause of it.  Even the link that you provided is less than clear as to how to use a class member as a ThreadProc.  Perhaps a B is justified here...

-- Dan
0
 
LVL 4

Expert Comment

by:mblat
ID: 7042693
2 DanRollins...

Perhaps...  
But it my mind it goes like this:

A. I have a problem with thread.
Q. Is that member of a class? You can't do that.
A. Why? What to to do about it?
Q. BlahBlah...

Basically why would I suggest solution if I am not even sure if that THE problem?  Besides solution was already suggested BEFORE I got into this conversation....

Any way, it's not like I care all that much, it's just justification of grade bothers ( somewhat ) me.

Peace....

:-)
0
 
LVL 19

Author Comment

by:Rimvis
ID: 7043788
To mblat, DanRollins

OK, maybe I made a mistake by not giving an 'A'. I'm new to this forum. When I was giving points, I didn't notice any notes about how grades affect points.

It's realy cool forum, and I'm looking forward to ask questions and give answers [maybe :o)] without conflicts. Again - my apologies.

Peace, OK? ;o)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

726 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