Although you may have your application set to handle critical errors, you probably won't handle every error that might occur. For those errors you don't handle, you'll need to inform the user that an error occurred. Fortunately, the FormatMessage function provides an easy way to do just that. This Win32 API function takes the error code returned from GetLastError and returns the error message text associated with that error code. Using FormatMessage, you can write a short function that displays the error message text for any Windows error. The function would look like this:
void TForm1::ShowErrorMessage(int code)
{
if (code == 0) return;
char buff[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
0, code, 0, buff, sizeof(buff), 0);
MessageBox(Handle, buff,
"System Error", MB_OK | MB_ICONWARNING);
}
Your error handling code, then, might resemble these lines:
SetErrorMode(SEM_FAILCRITICALERRORS); // Some code here which might cause an error int error = GetLastError(); if (error == ERROR_PATH_NOT_FOUND) // Do something! else ShowErrorMessage(error);Using such code, you can catch any errors that interest you and notify the user of any other errors that occur.