473,842 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to cast LPVOID to my class type??

52 New Member
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
4 9105
AdrianH
1,251 Recognized Expert Top Contributor
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_cas t 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
ppuniversal
52 New Member
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_cas t 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
1,251 Recognized Expert Top Contributor
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)(MyCla ss::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
ppuniversal
52 New Member
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)(MyCla ss::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

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

Similar topics

0
7392
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http channal hosting into IIS. There is a class B which is also available through remoting hosted on IIS on the same URI. B creates new of A inside a function. It succeed and able to create instance of A inside B first time. But it failes in 2nd attempt when...
6
2442
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and has an internal pointer to a list of pointer of Element type.
8
3655
by: Joe | last post by:
I have a web service which returns many types (classes) to match the return type. I want to cast the return type to the actual class type. For example: namespace Test { class MyClass { ...
3
4573
by: Charles Law | last post by:
I have the following lines Dim t As Type = GetType(MyType) Dim serialiser As New XmlSerializer(t) I want to be able to do the following with a FileStream fs Dim instance As MyType instance = CType(serialiser.Deserialize(fs), MyType)
8
6516
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
4
2466
by: JackBlack | last post by:
Hi, all! Need a little help tracking down a runtime error problem. I'm getting this error: "Unable to cast object of type 'myStruct' to type 'myStruct'... but the two types are identical! I have a class method that's building an array of user-defined structures (see below), and returning that array to the calling routine. I'm getting the error on that calling line. The structure in both the webform and class are defined like this:
3
1458
by: NorseMN | last post by:
I suppose my problem really started when I decided to implement a top-down design, because management always wants to see the frosting before they let you bake a cake. In any case, I now seem to have the proverbial design that is simple, elegant... and wrong. Before I give up, I'd like to see if anyone in this forum has a solution. I seems that I need to perform a run-time cast to a type that is specified by an integer. Ideally, the...
2
6550
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
1
2351
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The web.config has been updated with the necessary section. Using System.Web.Configuration.WebConfiguration.GetSection(), the config information is returned without any issues when the GetSection is set to an object. When the object is casted explicitly...
0
9717
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10950
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10617
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9459
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7862
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5886
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4506
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4096
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.