HighCommander4
September 23rd, 2006, 07:25 PM
When I try to convert the result of GetLastError() into a string error message using FormatMessage(), it always, without exception, returns 0 and a subsequent call to GetLastError() produces 8, which, according to MSDN, means out of memory. I don't see any reason why I would be constantly low of memory, my computer has 512 MB RAM, I get the error even if there are no other applications running, and I do not have any other memory problems (for example I don't get std::bad_alloc exceptions even though I use the STL extensively). Can anyone help me?
greg_dolley
September 23rd, 2006, 08:57 PM
Can you please post the code?
Greg Dolley
HighCommander4
September 23rd, 2006, 09:05 PM
Can you please post the code?
Greg Dolley
Here is the code I use to get an error message string based on an error code:
string LastError()
{
unsigned long error_code = ::GetLastError();
if (error_code == NOERROR)
return "No error";
const unsigned long format_control = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM;
void* buffer = NULL;
if (FormatMessage(format_control, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<char*>(buffer), 0, NULL) == 0)
return "Unknown error, error code is " + lexical_cast<string>(error_code) +
" (FormatMessage error code is " + lexical_cast<string>(::GetLastError()) + ")";
string result = const_cast<const char*>(reinterpret_cast<char*>(buffer));
::LocalFree(buffer);
return result;
}
The function always, without exception, returns "Unknown error, error code is [various error codes] (FormatMessage error code is 8)"