Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with LPTHREAD_START_ROUTINE

Posted on 2002-05-28
17
Medium Priority
?
2,132 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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