Solved

Problem with LPTHREAD_START_ROUTINE

Posted on 2002-05-28
17
2,057 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

912 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

21 Experts available now in Live!

Get 1:1 Help Now