Hi, how could the last error message be printed?
It gives me an integer instead of a text message.
Thanks.
Hi, how could the last error message be printed?
It gives me an integer instead of a text message.
Thanks.
FormatMessage will turn GetLastError's integer return into a text message.
In general, you need to use FormatMessage
to convert from a Win32 error code to text.
From the MSDN documentation:
Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
The declaration of FormatMessage:
DWORD WINAPI FormatMessage(
__in DWORD dwFlags,
__in_opt LPCVOID lpSource,
__in DWORD dwMessageId, // your error code
__in DWORD dwLanguageId,
__out LPTSTR lpBuffer,
__in DWORD nSize,
__in_opt va_list *Arguments
);
MSDN has some sample code that demonstrates how to use FormatMessage()
and GetLastError()
together: Retrieving the Last-Error Code