Hi,
can you tell me how to display the GetLastError() in a MessageBox()?
Thanks!
Printable View
Hi,
can you tell me how to display the GetLastError() in a MessageBox()?
Thanks!
Use FormatMessage() function to get error code string...
Thanks,
See also this FAQ
How to get the reason for a failure of a SDK function?.
GetLastError() return the calling thread's last-error code.
Error codes are 32-bit values so to display the error code you will have to construct a string representation of the error code, something like:
As indicated, You can obtain error message strings for system error codes using FormatMessage(), something like:Code:std::ostringstream ss;
DWORD dwLastError = ::GetLastError();
ss << dwLastError ;
YourMessageBox( ss.str(), ss.str().c_str() );
Code:int main()
{
void* lpBuffer;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpBuffer,
0,
NULL );
MessageBox( NULL, (LPCTSTR)lpBuffer, __T("LastRrror"), MB_OK );
LocalFree( lpBuffer );
return 0;
}
Regards
Doron Moraz