Click to See Complete Forum and Search --> : MessageBox() variables as parameters


tomkear2006
October 11th, 2009, 02:44 PM
I am having some embarrassing problems that I can't get my head around. I expect many people here will be able to help with this one.

I want a message box to output a variables content, but I am running into difficulties. My code basically goes something like this:

int iErrorCode = GetLastError();
char szErrorText[256];
ZeroMemory(szErrorText, sizeof(szErrorText));
sprintf(szErrorText, "Error Code Returned: %d", iErrorCode);

int iMessageReturn = MessageBox(NULL,
szErrorText,
L"Title of the box!",
MB_ICONERROR | MB_RETRYCANCEL);

I think this is something to do with the unicode setting but I don't know how to rectify. Can someone please help by telling me how I can convert the char variable into a LPCWSTR that the MessageBox() function won't moan about?

Thanks,
Tom

Igor Vartanov
October 11th, 2009, 03:08 PM
int iErrorCode = GetLastError();
TCHAR szErrorText[256];
ZeroMemory(szErrorText, sizeof(szErrorText));
stprintf(szErrorText, TEXT("Error Code Returned: %d"), iErrorCode);

int iMessageReturn = MessageBox(NULL,
szErrorText,
TEXT("Title of the box!"),
MB_ICONERROR | MB_RETRYCANCEL);

tomkear2006
October 11th, 2009, 03:14 PM
int iErrorCode = GetLastError();
TCHAR szErrorText[256];
ZeroMemory(szErrorText, sizeof(szErrorText));
stprintf(szErrorText, TEXT("Error Code Returned: %d"), iErrorCode);

int iMessageReturn = MessageBox(NULL,
szErrorText,
TEXT("Title of the box!"),
MB_ICONERROR | MB_RETRYCANCEL);

Awesome, thanks for the response.
What header do I need to include for stprintf() support?
Thanks again :D

EDIT: Appears to be working with _stprintf(). Presumably just a typo in the previous post!?

Krishnaa
October 12th, 2009, 04:39 AM
_stprintf() and stprintf() are the same.