Convert from DWORD to LPCWSTR

I am having an issue where I need to display a DWORD in a message box, but I cannot to save my life figure out how to do it. I know that MessageBox() takes in LPCWSTR and I do not know how to convert between the two. Can anyone help me?

-George
1
2
3
4
5
DWORD dw=3;
char szTest[10];
sprintf_s(szTest, "%d", dw);

MessageBox(hwnd, szTest, szAppName, MB_OK);


You may have to include stdio.h if you haven't already done so.

I tried that and the MessageBox() function sill gives me the error "error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [4]' to 'LPCWSTR'". This is so frustrating; I even undefined unicode and it didn't work. Any other advice?
I would need to see the code. If you have your progject as multi-byte you should be able to use a simple char[10] for your declaration or if unicode, TCHAR szTest[10].

Are you doing -- char whatever[10], or how are you declaring and defining your char?
LPCWSTR is a pointer to an address of wide characters (16 bits unlike char 8 bits).
An example to the use of MessageBox which is an alias to MessageBoxW.
1
2
3
4
5
DWORD dw=3;
WCHAR szTest[10]; // WCHAR is the same as wchar_t
// swprintf_s is the same as sprintf_s for wide characters
swprintf_s(szTest, 10, L"%d", dw); // use L"" prefix for wide chars
MessageBox(NULL, szTest, L"TEST", MB_OK); // a messageboy example again L as prefix 


[edit]changed sprintf to sprintf_s in the comment[/edit]
Last edited on
I tried jmc's code and it worked. Thanks!

-George
Topic archived. No new replies allowed.