473,804 Members | 2,139 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 9100
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
7390
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
2437
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
3654
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
4572
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
6514
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
2465
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
1452
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
6546
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
2350
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
9594
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
10600
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...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
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
7638
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
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.