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. ATL / WTL / STL
  4. Trying to Put HWND Value Into String [modified]

Trying to Put HWND Value Into String [modified]

Scheduled Pinned Locked Moved ATL / WTL / STL
c++questionlearning
11 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 Offline
    R Offline
    redfish34
    wrote on last edited by
    #1

    I am a beginner. I have read some tutorials and newbie books on C++ but am having trouble doing things beyond the basics. I am trying to display the HWND when i use certain Win32 functions such as GetForegroundWindow() for debugging purposes. When i use Win32 MessageBox or OutputDebugString such as below... 1) m_foreHwnd1 = GetForegroundWindow(); MessageBox(NULL, (LPCSTR)m_foreHwnd1, "INFO", MB_OK); 2) OutputDebugString("ForeHwnd1: "); OutputDebugString((LPCSTR)m_foreHwnd1); OutputDebugString("\n"); ... I get errors like the following: First-chance exception at 0x77e26673 in SendFocusWin.exe: 0xC0000005: Access violation reading location 0x00090548. Unhandled exception at 0x77e26673 in SendFocusWin.exe: 0xC0000005: Access violation reading location 0x00090548. I am not getting anywhere and am starting to get fustrated. What are the code steps i need to take to get the HWND value safely AND put it into a string variable that i can use elswhere in my code? What would be the ANSI solution?

    E 1 Reply Last reply
    0
    • R redfish34

      I am a beginner. I have read some tutorials and newbie books on C++ but am having trouble doing things beyond the basics. I am trying to display the HWND when i use certain Win32 functions such as GetForegroundWindow() for debugging purposes. When i use Win32 MessageBox or OutputDebugString such as below... 1) m_foreHwnd1 = GetForegroundWindow(); MessageBox(NULL, (LPCSTR)m_foreHwnd1, "INFO", MB_OK); 2) OutputDebugString("ForeHwnd1: "); OutputDebugString((LPCSTR)m_foreHwnd1); OutputDebugString("\n"); ... I get errors like the following: First-chance exception at 0x77e26673 in SendFocusWin.exe: 0xC0000005: Access violation reading location 0x00090548. Unhandled exception at 0x77e26673 in SendFocusWin.exe: 0xC0000005: Access violation reading location 0x00090548. I am not getting anywhere and am starting to get fustrated. What are the code steps i need to take to get the HWND value safely AND put it into a string variable that i can use elswhere in my code? What would be the ANSI solution?

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      why not you try CString instead? CString csHWND; csHWND.Format("%l",m_foreHwnd1); AfxMessageBox(csHWND); If you dont use MFC, then ltoa(m_foreHwnd1,szText,10); MessageBox(..szText..);


      --[:jig:]-- [My Current Status]

      R 1 Reply Last reply
      0
      • E Eytukan

        why not you try CString instead? CString csHWND; csHWND.Format("%l",m_foreHwnd1); AfxMessageBox(csHWND); If you dont use MFC, then ltoa(m_foreHwnd1,szText,10); MessageBox(..szText..);


        --[:jig:]-- [My Current Status]

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

        I am restricting my learning to ANSI for now. I canot believe how hard it is to convert a number into a string in C++. ltoa will only convert a long into a string, not HWND. I have searched on the internet and did not find a single code snippet to convert HWND into a string. Certainly programmers print out HWNDs in order to debug their code!? How does one convert HWND into string un ANSI C++?

        J E M 4 Replies Last reply
        0
        • R redfish34

          I am restricting my learning to ANSI for now. I canot believe how hard it is to convert a number into a string in C++. ltoa will only convert a long into a string, not HWND. I have searched on the internet and did not find a single code snippet to convert HWND into a string. Certainly programmers print out HWNDs in order to debug their code!? How does one convert HWND into string un ANSI C++?

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #4

          You're not supposed to be able to convert a HWND to a string. Of course, if you treat it as a pointer, you'll get away with it. ;P

          CString str; str.Format(_T("%p"), hWnd);

          But why you'd want that string, is beyond me. Maybe you want the window's caption/title instead? ::GetWindowText()...

          -- Not Y3K Compliant

          R 1 Reply Last reply
          0
          • J Jorgen Sigvardsson

            You're not supposed to be able to convert a HWND to a string. Of course, if you treat it as a pointer, you'll get away with it. ;P

            CString str; str.Format(_T("%p"), hWnd);

            But why you'd want that string, is beyond me. Maybe you want the window's caption/title instead? ::GetWindowText()...

            -- Not Y3K Compliant

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

            I found the answer. You folks have really played the tempting devil with your MFC code, but i am going to restrict my learning to ANSI C++/STL. I plan to develop on Linux/Mac too. The following code works. char buffer [100]; sprintf(buffer, "%I64d \n", (unsigned __int64) hwnd); OutputDebugString(buffer); The reason i want the HWND in a string is so i can dump debug info to troubleshoot a problem. It seems that the hardest part of C++ is finding the information! By luck i found the answer on the internet. I had no clue that sprintf was to be used for this. I come from VB6/C# background and so these non-orthodox language commands throw me off. Once i learn the C++ quirks things will become straight forward.

            J M 2 Replies Last reply
            0
            • R redfish34

              I am restricting my learning to ANSI for now. I canot believe how hard it is to convert a number into a string in C++. ltoa will only convert a long into a string, not HWND. I have searched on the internet and did not find a single code snippet to convert HWND into a string. Certainly programmers print out HWNDs in order to debug their code!? How does one convert HWND into string un ANSI C++?

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              Hi redfish, I'm sorry I missed to cast it (if you use ltoa). if you use CString the matter is much more simple. Try this simple one: CString cs; cs.Format("%x",this->m_hWnd); AfxMessageBox(cs); --------------OR--------------------------------- char ch[10]; ltoa((long)this->m_hWnd,ch,16); AfxMessageBox(ch); This will give you the exact format that you will find in the spy++.

              redfish34 wrote:

              I canot believe how hard it is to convert a number into a string in C++.

              but have a look at the 3rd parameter, it gives you the flexibility to convert to base 2(bin), 16(hex), 10(deci) etc.. Dont you find it useful?


              --[:jig:]-- [My Current Status]

              1 Reply Last reply
              0
              • R redfish34

                I am restricting my learning to ANSI for now. I canot believe how hard it is to convert a number into a string in C++. ltoa will only convert a long into a string, not HWND. I have searched on the internet and did not find a single code snippet to convert HWND into a string. Certainly programmers print out HWNDs in order to debug their code!? How does one convert HWND into string un ANSI C++?

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #7

                redfish34 wrote:

                ltoa will only convert a long into a string, not HWND.

                We have similar handle in our SDK which is not a struct, I typed it having that in mind. sorry ;)


                --[:jig:]-- [My Current Status]

                1 Reply Last reply
                0
                • R redfish34

                  I found the answer. You folks have really played the tempting devil with your MFC code, but i am going to restrict my learning to ANSI C++/STL. I plan to develop on Linux/Mac too. The following code works. char buffer [100]; sprintf(buffer, "%I64d \n", (unsigned __int64) hwnd); OutputDebugString(buffer); The reason i want the HWND in a string is so i can dump debug info to troubleshoot a problem. It seems that the hardest part of C++ is finding the information! By luck i found the answer on the internet. I had no clue that sprintf was to be used for this. I come from VB6/C# background and so these non-orthodox language commands throw me off. Once i learn the C++ quirks things will become straight forward.

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #8

                  redfish34 wrote:

                  You folks have really played the tempting devil with your MFC code

                  Huh? The MFC's CString uses the same formatting specifiers as the printf-functions in the C library.

                  redfish34 wrote:

                  char buffer [100]; sprintf(buffer, "%I64d \n", (unsigned __int64) hwnd); OutputDebugString(buffer);

                  Why not use %p?? (Which incidently is standard C, and thus standard C++, while %I64d isn't).

                  redfish34 wrote:

                  The reason i want the HWND in a string is so i can dump debug info to troubleshoot a problem.

                  For your own development purposes, I presume? The handle value itself tells you nothing once the window has been destroyed. Nothing, nada, zilch.

                  -- Larva-Tested, Pupa-Approved

                  E 1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    redfish34 wrote:

                    You folks have really played the tempting devil with your MFC code

                    Huh? The MFC's CString uses the same formatting specifiers as the printf-functions in the C library.

                    redfish34 wrote:

                    char buffer [100]; sprintf(buffer, "%I64d \n", (unsigned __int64) hwnd); OutputDebugString(buffer);

                    Why not use %p?? (Which incidently is standard C, and thus standard C++, while %I64d isn't).

                    redfish34 wrote:

                    The reason i want the HWND in a string is so i can dump debug info to troubleshoot a problem.

                    For your own development purposes, I presume? The handle value itself tells you nothing once the window has been destroyed. Nothing, nada, zilch.

                    -- Larva-Tested, Pupa-Approved

                    E Offline
                    E Offline
                    Eytukan
                    wrote on last edited by
                    #9

                    RedFish wrote:

                    "%I64d \n", (unsigned __int64) hwnd);

                    looks really weired. X|


                    --[:jig:]-- [My Current Status] -- modified at 10:18 Wednesday 5th July, 2006

                    1 Reply Last reply
                    0
                    • R redfish34

                      I am restricting my learning to ANSI for now. I canot believe how hard it is to convert a number into a string in C++. ltoa will only convert a long into a string, not HWND. I have searched on the internet and did not find a single code snippet to convert HWND into a string. Certainly programmers print out HWNDs in order to debug their code!? How does one convert HWND into string un ANSI C++?

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

                      redfish34 wrote:

                      I canot believe how hard it is to convert a number into a string in C++.

                      There are several ways, depending on what class library you prefer. See the FAQ: 6.3 How can I change a number into its string representation, or vice versa? [^]

                      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                      1 Reply Last reply
                      0
                      • R redfish34

                        I found the answer. You folks have really played the tempting devil with your MFC code, but i am going to restrict my learning to ANSI C++/STL. I plan to develop on Linux/Mac too. The following code works. char buffer [100]; sprintf(buffer, "%I64d \n", (unsigned __int64) hwnd); OutputDebugString(buffer); The reason i want the HWND in a string is so i can dump debug info to troubleshoot a problem. It seems that the hardest part of C++ is finding the information! By luck i found the answer on the internet. I had no clue that sprintf was to be used for this. I come from VB6/C# background and so these non-orthodox language commands throw me off. Once i learn the C++ quirks things will become straight forward.

                        M Offline
                        M Offline
                        Mattias G
                        wrote on last edited by
                        #11

                        Try this char buf[16]; sprintf(buf, "0x%08x", hwnd); OutputDebugString(buf); You wont need 64 bit integers for your HWNDs. -- modified at 18:59 Wednesday 5th July, 2006

                        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