FormatMessage for unicode
-
Windows 10, Visual Studio 2012,C++ I need a version of FormatMessage() for Unicode, WCHAR. Google and MSDN deny all my searches for same. I want to call a function of my own that accepts a pointer to WCHAR and the error number, and puts the resultant string in the passed array. Lacking that, I need a version of swprintf() that has an argument for a max number of characters to move into the string and accepts the LPSTR that is returned by FormatMessage().
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
Windows 10, Visual Studio 2012,C++ I need a version of FormatMessage() for Unicode, WCHAR. Google and MSDN deny all my searches for same. I want to call a function of my own that accepts a pointer to WCHAR and the error number, and puts the resultant string in the passed array. Lacking that, I need a version of swprintf() that has an argument for a max number of characters to move into the string and accepts the LPSTR that is returned by FormatMessage().
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
I presume you mean something like:
void ShowError(DWORD dwError,
PWSTR pszMessage,
int maxMessageSize
)
{
if (dwError == 0)
{
dwError = GetLastError();
}
if (FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK, // don't include line breaks
NULL, // no source message
dwError, // message identifier
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
pszMessage, // address of message buffer
maxMessageSize, // size of user supplied buffer
NULL // optional parameter list
) == 0)
{
// unknown error code
swprintf_s(pszMessage, maxMessageSize, L"Error code: %d (0x%X)", dwError, dwError);
}
}Which you call by:
WCHAR szMessage\[512\]; ShowError(nErrorCode, szMessage, \_countof(szMessage)); wcout << "Exception: " << szMessage << endl;