tags:

views:

1163

answers:

4

Hi, how could the last error message be printed?

It gives me an integer instead of a text message.

Thanks.

+5  A: 

FormatMessage will turn GetLastError's integer return into a text message.

wrang-wrang
+2  A: 

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
);
Vinay Sajip
A: 
You should edit your question and add this information instead of posting it as an answer.
bk1e
The sample linked to by bk1e is better, as it gets `FormatMessage` to allocate the buffer for you. (And it's smart enough to allocate as big of a buffer as it needs.) Just make sure you free the allocated buffer using LocalFree.
Reuben
+1  A: 

MSDN has some sample code that demonstrates how to use FormatMessage() and GetLastError() together: Retrieving the Last-Error Code

bk1e