Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing classes into threads

Passing classes into threads

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R roadragedave

    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??????

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    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)

    R 1 Reply Last reply
    0
    • D David Crow

      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)

      R Offline
      R Offline
      roadragedave
      wrote on last edited by
      #3

      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 parameter LPVOID 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!

      D 1 Reply Last reply
      0
      • R roadragedave

        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 parameter LPVOID 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!

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        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)

        R 1 Reply Last reply
        0
        • D David Crow

          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)

          R Offline
          R Offline
          roadragedave
          wrote on last edited by
          #5

          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!

          D 1 Reply Last reply
          0
          • R roadragedave

            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!

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            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)

            R 1 Reply Last reply
            0
            • D David Crow

              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)

              R Offline
              R Offline
              roadragedave
              wrote on last edited by
              #7

              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!

              D 1 Reply Last reply
              0
              • R roadragedave

                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!

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                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)

                R 1 Reply Last reply
                0
                • D David Crow

                  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)

                  R Offline
                  R Offline
                  roadragedave
                  wrote on last edited by
                  #9

                  Fantastic!!!! We have a mathematician, a different kind of mathematician, and a statistician!

                  1 Reply Last reply
                  0
                  • R roadragedave

                    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??????

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #10

                    CreateThread() has a param just for this purpose: void* lpParameter. Set that to the address of the Wmi object. loadPerThrdFunc takes one parameter, again a void*, which you cast back to a WmiWrapper* Note that you should be using _beginthreadex() and not CreateThread() 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.

                    1 Reply Last reply
                    0
                    • R roadragedave

                      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??????

                      J Offline
                      J Offline
                      Jitendra gangwar
                      wrote on last edited by
                      #11

                      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

                      R 1 Reply Last reply
                      0
                      • J Jitendra gangwar

                        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

                        R Offline
                        R Offline
                        roadragedave
                        wrote on last edited by
                        #12

                        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, not DWORD, 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!

                        P J 2 Replies Last reply
                        0
                        • R roadragedave

                          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, not DWORD, 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!

                          P Offline
                          P Offline
                          Paul Ranson
                          wrote on last edited by
                          #13

                          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

                          1 Reply Last reply
                          0
                          • R roadragedave

                            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, not DWORD, 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!

                            J Offline
                            J Offline
                            Jitendra gangwar
                            wrote on last edited by
                            #14

                            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

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups