By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

How to cast LPVOID to my class type??

P: 52
Hello,
I am making a thread program, in which i call :

Expand|Select|Wrap|Line Numbers
  1. hThread = CreateThread(    NULL,
  2.                         0,
  3.             (LPTHREAD_START_ROUTINE)(this->runThread),
  4.             this,
  5.             0,
  6.             &ThreadId);
  7.  
in the constructor of my class.

Here hThread is the handle to the thread created.
And runThread is my thread function.

The 4th parameter passed is the pointer to my current object, i.e. "this".

The prototype of my runThread function is:
Expand|Select|Wrap|Line Numbers
  1. static DWORD WINAPI runThread(LPVOID param);
  2.  

Now the problem is:

In the runThread function when I convert the "param" to my class type i.e.
Expand|Select|Wrap|Line Numbers
  1. MyClass ob = (MyClass)param;
  2.  
it gives the following error on compilation:

Expand|Select|Wrap|Line Numbers
  1. error C2440: 'type cast' : cannot convert from 'LPVOID' to 'MyClass'
  2.  
Please tell either how to overcome this problem or any other way of passing my object to the runThread function.
Pawan
Feb 20 '07 #1
Share this Question
Share on Google+
4 Replies


AdrianH
Expert 100+
P: 1,251
In the runThread function when I convert the "param" to my class type i.e.
Expand|Select|Wrap|Line Numbers
  1. MyClass ob = (MyClass)param;
  2.  
it gives the following error on compilation:

Expand|Select|Wrap|Line Numbers
  1. error C2440: 'type cast' : cannot convert from 'LPVOID' to 'MyClass'
  2.  
Please tell either how to overcome this problem or any other way of passing my object to the runThread function.
Pawan
The problem is that you are trying to reinterpret a type to another type, and the types have nothing in common, especially size. this is a pointer, you need to do a reinterpret_cast from param to a pointer to the class, like one of the following:
Expand|Select|Wrap|Line Numbers
  1. MyClass* pOb = reinterpret_cast<MyClass*>(param);
  2. MyClass& rOb = *reinterpret_cast<MyClass*>(param);
  3.  
The first casts to a MyClass * and assigns it to pOb. The second does the same cast but dereferences it with the '*' operator and is then assigned to a reference variable.

Does this make sense? Hope it helps.


Adrian
Feb 20 '07 #2

P: 52
The problem is that you are trying to reinterpret a type to another type, and the types have nothing in common, especially size. this is a pointer, you need to do a reinterpret_cast from param to a pointer to the class, like one of the following:
Expand|Select|Wrap|Line Numbers
  1. MyClass* pOb = reinterpret_cast<MyClass*>(param);
  2. MyClass& rOb = *reinterpret_cast<MyClass*>(param);
  3.  
The first casts to a MyClass * and assigns it to pOb. The second does the same cast but dereferences it with the '*' operator and is then assigned to a reference variable.

Does this make sense? Hope it helps.


Adrian
I used your method, and it compiled without errors, but when the MyClass.exe was executed, it resulted in fatal error. Then I tried simple:
Expand|Select|Wrap|Line Numbers
  1. MyClass *ob = (MyClass *)param;
  2.  
and it worked for me.

But, I believe and have read on net that the your told
Expand|Select|Wrap|Line Numbers
  1. MyClass* pOb = reinterpret_cast<MyClass*>(param);
  2. MyClass& rOb = *reinterpret_cast<MyClass*>(param);
  3.  
are totally correct, it is me who is doing some wrong thing in my code.

Still, thanks for your detail answer, I will use it in future programs.
Pawan
Feb 21 '07 #3

AdrianH
Expert 100+
P: 1,251
I used your method, and it compiled without errors, but when the MyClass.exe was executed, it resulted in fatal error. Then I tried simple:
Expand|Select|Wrap|Line Numbers
  1. MyClass *ob = (MyClass *)param;
  2.  
and it worked for me.

But, I believe and have read on net that the your told
Expand|Select|Wrap|Line Numbers
  1. MyClass* pOb = reinterpret_cast<MyClass*>(param);
  2. MyClass& rOb = *reinterpret_cast<MyClass*>(param);
  3.  
are totally correct, it is me who is doing some wrong thing in my code.

Still, thanks for your detail answer, I will use it in future programs.
Pawan
Odd, should work. What compiler are you using? Version number?

Also, your use of (LPTHREAD_START_ROUTINE)(this->runThread), is not necessary. Since runThread() is a static member function, you could just use (LPTHREAD_START_ROUTINE)(MyClass::runThread), it doesn't need the this context. In fact if you are using this and it is working it means you are calling CreateThread() from within the class so then you don't even need MyClass:: either as that static member function is implied.

Glad to help.


Adrian
Feb 21 '07 #4

P: 52
Odd, should work. What compiler are you using? Version number?

Also, your use of (LPTHREAD_START_ROUTINE)(this->runThread), is not necessary. Since runThread() is a static member function, you could just use (LPTHREAD_START_ROUTINE)(MyClass::runThread), it doesn't need the this context. In fact if you are using this and it is working it means you are calling CreateThread() from within the class so then you don't even need MyClass:: either as that static member function is implied.

Glad to help.


Adrian
Yes, whatever you said is correct, I did called runThread directly after some time and it worked.

For information, I am using:

Microsoft Visual Studio 2005
Version 8.0.50727.42

I dont know why the error came. But now it is working fine with the way I used,
i.e. MyClass *ob = (MyClass *)param;


Pawan
Feb 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.