tags:

views:

36

answers:

1

How to print HWND value to MessageBox in visual c++?

Update:

I tried to print the handle ID to a messagebox, but instead it appears with Chinese characters. Here's the code I'm working on ..

        char szBuff[64];
        sprintf(szBuff, "%p", m_hWnd);
        MessageBox(NULL, LPCWSTR(szBuff), L"Test print handler", MB_OK);
+2  A: 

A HWND is just a pointer.

char szBuff[64];
sprintf(szBuff, "%p", hWnd);
MessageBox(NULL, szBuff, "Title", MB_OK);

Update
Sounds like you are having trouble with wide and narrow characters (ASCII and UTF-16). Try the following:

#include <Windows.h>
#include <cstdio>

int _tmain(int argc, TCHAR* argv[])
{
    HWND hWnd=::GetConsoleWindow();

    TCHAR szBuff[64];
    _stprintf(szBuff, _T("%p"), hWnd);
    MessageBox(NULL, szBuff, _T("Title"), MB_OK);

    return 0;
}
Michael J
thanks for the help
barlyee
This error appears. May I know how to solve it? : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [64]' to 'LPCWSTR'
barlyee