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;