Hello,
I am making a thread program, in which i call : -
hThread = CreateThread( NULL,
-
0,
-
(LPTHREAD_START_ROUTINE)(this->runThread),
-
this,
-
0,
-
&ThreadId);
-
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: -
static DWORD WINAPI runThread(LPVOID param);
-
Now the problem is:
In the runThread function when I convert the "param" to my class type i.e. -
MyClass ob = (MyClass)param;
-
it gives the following error on compilation: -
error C2440: 'type cast' : cannot convert from 'LPVOID' to 'MyClass'
-
Please tell either how to overcome this problem or any other way of passing my object to the runThread function.
Pawan
4 9100 AdrianH 1,251
Recognized Expert Top Contributor
In the runThread function when I convert the "param" to my class type i.e. -
MyClass ob = (MyClass)param;
-
it gives the following error on compilation: -
error C2440: 'type cast' : cannot convert from 'LPVOID' to 'MyClass'
-
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: -
MyClass* pOb = reinterpret_cast<MyClass*>(param);
-
MyClass& rOb = *reinterpret_cast<MyClass*>(param);
-
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
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: -
MyClass* pOb = reinterpret_cast<MyClass*>(param);
-
MyClass& rOb = *reinterpret_cast<MyClass*>(param);
-
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: -
MyClass *ob = (MyClass *)param;
-
and it worked for me.
But, I believe and have read on net that the your told -
MyClass* pOb = reinterpret_cast<MyClass*>(param);
-
MyClass& rOb = *reinterpret_cast<MyClass*>(param);
-
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
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: -
MyClass *ob = (MyClass *)param;
-
and it worked for me.
But, I believe and have read on net that the your told -
MyClass* pOb = reinterpret_cast<MyClass*>(param);
-
MyClass& rOb = *reinterpret_cast<MyClass*>(param);
-
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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
{
...
|
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)
|
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
| |
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:
|
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...
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |