Trying to Put HWND Value Into String [modified]
-
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?
-
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?
why not you try CString instead?
CString csHWND; csHWND.Format("%l",m_foreHwnd1); AfxMessageBox(csHWND);
If you dont use MFC, thenltoa(m_foreHwnd1,szText,10); MessageBox(..szText..);
--[:jig:]-- [My Current Status]
-
why not you try CString instead?
CString csHWND; csHWND.Format("%l",m_foreHwnd1); AfxMessageBox(csHWND);
If you dont use MFC, thenltoa(m_foreHwnd1,szText,10); MessageBox(..szText..);
--[:jig:]-- [My Current Status]
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++?
-
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++?
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
-
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
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.
-
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++?
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]
-
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++?
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]
-
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.
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
-
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
RedFish wrote:
"%I64d \n", (unsigned __int64) hwnd);
looks really weired. X|
--[:jig:]-- [My Current Status] -- modified at 10:18 Wednesday 5th July, 2006
-
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++?
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
-
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.