Passing classes into threads
-
Im creating threads and I want to pass a pointer to the threads, Is is possible to cast a pointer to a class into a DWORD, if so, how is it done??? Here is the code Im working with, its taken straight from the MSDN
void main(void){ DWORD loadPerThrdID; HANDLE loadPerThrd; TCHAR szMsg[80]; DWORD dwThrdParam; static WmiWrapper Wmi(NULL, NULL, NULL); //Creating object loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, &dwThrdParam, 0, &loadPerThrdID); if(loadPerThrd == NULL){ wsprintf(szMsg, (TEXT("Create Thread failed for LoadPercentage"))); MessageBox(NULL, szMsg, NULL, MB_OK); } else{ _getch(); CloseHandle(loadPerThrd); } } //Threads function DWORD WINAPI loadPerThrdFunc (WmiWrapper Wmi) { ................ }
What can I do to get this to work, or am I completly out??????roadragedave wrote: Is is possible to cast a pointer to a class into a DWORD, if so, how is it done??? Both a pointer and a DWORD are 32-bit values.
DWORD WINAPI loadPerThrdFunc( LPVOID param )
{
DWORD *p = (DWORD *) param;
TCHAR s[32];
wsprintf(s, "The thread value is %lu\n", *p)
MessageBox(s);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
roadragedave wrote: Is is possible to cast a pointer to a class into a DWORD, if so, how is it done??? Both a pointer and a DWORD are 32-bit values.
DWORD WINAPI loadPerThrdFunc( LPVOID param )
{
DWORD *p = (DWORD *) param;
TCHAR s[32];
wsprintf(s, "The thread value is %lu\n", *p)
MessageBox(s);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
Im still lost, You said that pointers and DWORD are 32 bits, but when I build the app, the following line warnss me that there is a possible mismatch
DWORD dwThrdParam = UINT_PTR(&Wmi);
gives me:warning C4244: 'initializing' : conversion from '__**w64** unsigned int' to 'DWORD', possible loss of data
but otherwise it passes fine, but how do I turn the parameterLPVOID param
back into the object or at least a pointer to the object???? We have a mathematician, a different kind of mathematician, and a statistician! -
Im still lost, You said that pointers and DWORD are 32 bits, but when I build the app, the following line warnss me that there is a possible mismatch
DWORD dwThrdParam = UINT_PTR(&Wmi);
gives me:warning C4244: 'initializing' : conversion from '__**w64** unsigned int' to 'DWORD', possible loss of data
but otherwise it passes fine, but how do I turn the parameterLPVOID param
back into the object or at least a pointer to the object???? We have a mathematician, a different kind of mathematician, and a statistician!roadragedave wrote: You said that pointers and DWORD are 32 bits... Most of the time they are. You are apparently doing some 64-bit work.
DWORD dwThrdParam = (DWORD) UINT_PTR(&Wmi); // potential loss of data
or__w64 dwThrdParam = UINT_PTR(&Wmi);
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
roadragedave wrote: You said that pointers and DWORD are 32 bits... Most of the time they are. You are apparently doing some 64-bit work.
DWORD dwThrdParam = (DWORD) UINT_PTR(&Wmi); // potential loss of data
or__w64 dwThrdParam = UINT_PTR(&Wmi);
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
Excellent stuff, that got rid of the warning, but again how, do I change it back inside the thread function We have a mathematician, a different kind of mathematician, and a statistician!
-
Excellent stuff, that got rid of the warning, but again how, do I change it back inside the thread function We have a mathematician, a different kind of mathematician, and a statistician!
Was the example I provided earlier incorrect? If so, how is
CreateThread()
currently being called, and what does your thread function currently look like?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Was the example I provided earlier incorrect? If so, how is
CreateThread()
currently being called, and what does your thread function currently look like?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
But how, by using this:
DWORD WINAPI loadPerThrdFunc( LPVOID param ) { DWORD *p = (DWORD *) param; TCHAR s[32]; wsprintf(s, "The thread value is %lu\n", *p) MessageBox(s); }
can I get to call the methods of the WmiWrapper class Wmi Im calling the CreateThread method like so:DWORD loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, &dwThrdParam, 0, &loadPerThrdID);
We have a mathematician, a different kind of mathematician, and a statistician! -
But how, by using this:
DWORD WINAPI loadPerThrdFunc( LPVOID param ) { DWORD *p = (DWORD *) param; TCHAR s[32]; wsprintf(s, "The thread value is %lu\n", *p) MessageBox(s); }
can I get to call the methods of the WmiWrapper class Wmi Im calling the CreateThread method like so:DWORD loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, &dwThrdParam, 0, &loadPerThrdID);
We have a mathematician, a different kind of mathematician, and a statistician!DWORD loadPerThrd = CreateThread(
NULL, 0,
loadPerThrdFunc,
&Wmi,
0, &loadPerThrdID);DWORD WINAPI loadPerThrdFunc( LPVOID param )
{
WmiWrapper *p = (WmiWrapper *) param;
p->...
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
DWORD loadPerThrd = CreateThread(
NULL, 0,
loadPerThrdFunc,
&Wmi,
0, &loadPerThrdID);DWORD WINAPI loadPerThrdFunc( LPVOID param )
{
WmiWrapper *p = (WmiWrapper *) param;
p->...
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
Fantastic!!!! We have a mathematician, a different kind of mathematician, and a statistician!
-
Im creating threads and I want to pass a pointer to the threads, Is is possible to cast a pointer to a class into a DWORD, if so, how is it done??? Here is the code Im working with, its taken straight from the MSDN
void main(void){ DWORD loadPerThrdID; HANDLE loadPerThrd; TCHAR szMsg[80]; DWORD dwThrdParam; static WmiWrapper Wmi(NULL, NULL, NULL); //Creating object loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, &dwThrdParam, 0, &loadPerThrdID); if(loadPerThrd == NULL){ wsprintf(szMsg, (TEXT("Create Thread failed for LoadPercentage"))); MessageBox(NULL, szMsg, NULL, MB_OK); } else{ _getch(); CloseHandle(loadPerThrd); } } //Threads function DWORD WINAPI loadPerThrdFunc (WmiWrapper Wmi) { ................ }
What can I do to get this to work, or am I completly out??????CreateThread()
has a param just for this purpose:void* lpParameter
. Set that to the address of the Wmi object.loadPerThrdFunc
takes one parameter, again avoid*
, which you cast back to aWmiWrapper*
Note that you should be using_beginthreadex()
and notCreateThread()
if you link with the C runtime (which you probably are) --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Windows troubleshooting: Reboot first, ask questions later. -
Im creating threads and I want to pass a pointer to the threads, Is is possible to cast a pointer to a class into a DWORD, if so, how is it done??? Here is the code Im working with, its taken straight from the MSDN
void main(void){ DWORD loadPerThrdID; HANDLE loadPerThrd; TCHAR szMsg[80]; DWORD dwThrdParam; static WmiWrapper Wmi(NULL, NULL, NULL); //Creating object loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, &dwThrdParam, 0, &loadPerThrdID); if(loadPerThrd == NULL){ wsprintf(szMsg, (TEXT("Create Thread failed for LoadPercentage"))); MessageBox(NULL, szMsg, NULL, MB_OK); } else{ _getch(); CloseHandle(loadPerThrd); } } //Threads function DWORD WINAPI loadPerThrdFunc (WmiWrapper Wmi) { ................ }
What can I do to get this to work, or am I completly out??????Did some modification in your existing code. This would help you. For the passing parameter to thread, first cast it to DWORD and receive it in callback handler of thread,cast it back to its original type,now use it as you want. void main(void){ DWORD loadPerThrdID; HANDLE loadPerThrd; TCHAR szMsg[80]; DWORD dwThrdParam; static WmiWrapper Wmi(NULL, NULL, NULL); //Creating object loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, //&dwThrdParam, (DWORD) &Wmi, //Pass the object pointer as a thread parameter. 0, &loadPerThrdID); if(loadPerThrd == NULL){ wsprintf(szMsg, (TEXT("Create Thread failed for LoadPercentage"))); MessageBox(NULL, szMsg, NULL, MB_OK); } else{ _getch(); CloseHandle(loadPerThrd); } } //Threads function DWORD WINAPI loadPerThrdFunc (DWORD param1) { WmiWrapper* wmi = (WmiWrapper*) param1; //Now you can use this wmi class pointer. ................ } Jitendra
-
Did some modification in your existing code. This would help you. For the passing parameter to thread, first cast it to DWORD and receive it in callback handler of thread,cast it back to its original type,now use it as you want. void main(void){ DWORD loadPerThrdID; HANDLE loadPerThrd; TCHAR szMsg[80]; DWORD dwThrdParam; static WmiWrapper Wmi(NULL, NULL, NULL); //Creating object loadPerThrd = CreateThread( NULL, 0, loadPerThrdFunc, //&dwThrdParam, (DWORD) &Wmi, //Pass the object pointer as a thread parameter. 0, &loadPerThrdID); if(loadPerThrd == NULL){ wsprintf(szMsg, (TEXT("Create Thread failed for LoadPercentage"))); MessageBox(NULL, szMsg, NULL, MB_OK); } else{ _getch(); CloseHandle(loadPerThrd); } } //Threads function DWORD WINAPI loadPerThrdFunc (DWORD param1) { WmiWrapper* wmi = (WmiWrapper*) param1; //Now you can use this wmi class pointer. ................ } Jitendra
No good, First I think the WmiWrapper* is 64 bits, where as DWORD is 32, so this gives me data loss warnings, Also the parameter is
LPTHREAD_START_ROUTINE
, notDWORD
, so this gives me an error. Its alright because I have it fixed (see David Crows code above), but now Im getting a memory voilation when running, and its in one of the classes methods, I dont understand, because I was running the program without threads before and I didnt get this problem, I thought mabey it has something to do with the fact that Im creating two threads which pass the same object, but if I turn off one I still get the error:confused: We have a mathematician, a different kind of mathematician, and a statistician! -
No good, First I think the WmiWrapper* is 64 bits, where as DWORD is 32, so this gives me data loss warnings, Also the parameter is
LPTHREAD_START_ROUTINE
, notDWORD
, so this gives me an error. Its alright because I have it fixed (see David Crows code above), but now Im getting a memory voilation when running, and its in one of the classes methods, I dont understand, because I was running the program without threads before and I didnt get this problem, I thought mabey it has something to do with the fact that Im creating two threads which pass the same object, but if I turn off one I still get the error:confused: We have a mathematician, a different kind of mathematician, and a statistician!The 'WmiWrapper*' can only be 64 bits if you're compiling for 64 bit Windows. You would know this. I think you get the warning because MS have made the SDK headers 64 bit compatible and introduced some new MS special bits to allow this. In the future a DWORD won't be compatible with a pointer. Anyway the parameter to CreateThread is 'LPVOID' not DWORD so in 64bit Windows that will be a 64 bit pointer and all will just work. Anyway I would expect something like
DWORD WINAPI loadPerThrdFunc ( LPVOID param )
{
WmiWrapper* wmi = reinterpret_Cast( param );
//...
}
//...
loadPerThrd = CreateThread (
NULL,
0,
loadPerThrdFunc,
&Wmi,
0,
&loadPerThrdID);Paul
-
No good, First I think the WmiWrapper* is 64 bits, where as DWORD is 32, so this gives me data loss warnings, Also the parameter is
LPTHREAD_START_ROUTINE
, notDWORD
, so this gives me an error. Its alright because I have it fixed (see David Crows code above), but now Im getting a memory voilation when running, and its in one of the classes methods, I dont understand, because I was running the program without threads before and I didnt get this problem, I thought mabey it has something to do with the fact that Im creating two threads which pass the same object, but if I turn off one I still get the error:confused: We have a mathematician, a different kind of mathematician, and a statistician!That is all right.Thanks for correcting me. The memory violation that you are facing may be due to that you are creating a local object.when you are creating the thread and passing the pointer of it. but after creation of thread, the control come back in the main function and the local object will got out of scope.still the thread is running and trying to access it by the pointer and cause the memory violation. if this is a problem then you can allocate the object by 'new' operator. so when you don't want it you can deallocate it manually. Jitendra