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. Convert DWORD_PTR back to my object

Convert DWORD_PTR back to my object

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
7 Posts 4 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.
  • P Offline
    P Offline
    paresh_joe
    wrote on last edited by
    #1

    I have problem with Casting ... i've create class named Instrument { ... } and in Dialog based MFC application i have TreeView Control and in that at some point i am settin ItemData as "Instument" classs' Object treeCtrl->SetItemData(instObject)//Instrument* to DWORD_PTR casting is done here ;) now at some point i need this data(object) back so to take data Instrument *instObject = treeCtrl->GetItemData(hItem); // error: :^) Please help me to cast DWORD_PTR back to Instrument Type ...:(

    M 1 Reply Last reply
    0
    • P paresh_joe

      I have problem with Casting ... i've create class named Instrument { ... } and in Dialog based MFC application i have TreeView Control and in that at some point i am settin ItemData as "Instument" classs' Object treeCtrl->SetItemData(instObject)//Instrument* to DWORD_PTR casting is done here ;) now at some point i need this data(object) back so to take data Instrument *instObject = treeCtrl->GetItemData(hItem); // error: :^) Please help me to cast DWORD_PTR back to Instrument Type ...:(

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

      To reverse the effect of the first cast, cast the value back to the original type:

      Instrument *instObject = (Instrument*) treeCtrl->GetItemData(hItem);

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

      P 1 Reply Last reply
      0
      • M Michael Dunn

        To reverse the effect of the first cast, cast the value back to the original type:

        Instrument *instObject = (Instrument*) treeCtrl->GetItemData(hItem);

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

        P Offline
        P Offline
        paresh_joe
        wrote on last edited by
        #3

        hey its not working ... otherwise i would have done that ...

        D 1 Reply Last reply
        0
        • P paresh_joe

          hey its not working ... otherwise i would have done that ...

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

          Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          C P 2 Replies Last reply
          0
          • D David Crow

            Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            C Offline
            C Offline
            carrivick
            wrote on last edited by
            #5

            I'm guessing you are getting cannot convert DWORD_PTR to Instrument *. Oh and allways try and use eplicit casting i.e. static_cast rather than C-Style casts. Try this Instrument *instObject = static_cast((static_cast(treeCtrl->GetItemData(hItem)))); or if not Instrument *instObject = dynamic_cast((dynamic_cast(treeCtrl->GetItemData(hItem)))); Could you copy the exact compiler error into the thread.

            M 1 Reply Last reply
            0
            • D David Crow

              Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              P Offline
              P Offline
              paresh_joe
              wrote on last edited by
              #6

              oh sorry its working now ... it was my mistake ... Thanks a lot ... :-D

              1 Reply Last reply
              0
              • C carrivick

                I'm guessing you are getting cannot convert DWORD_PTR to Instrument *. Oh and allways try and use eplicit casting i.e. static_cast rather than C-Style casts. Try this Instrument *instObject = static_cast((static_cast(treeCtrl->GetItemData(hItem)))); or if not Instrument *instObject = dynamic_cast((dynamic_cast(treeCtrl->GetItemData(hItem)))); Could you copy the exact compiler error into the thread.

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

                reinterpret_cast is the right one for this situation, since you're converting between a pointer and a non-pointer type.

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ I work for Keyser Söze

                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