2005-04-10

Carbon: Error Message

In Carbon, most functions return error as OSStatus integer code. In all code I have seen so far, the programmer has to expect certain result codes and supply custom error messages to the user:


OSStatus err = Call(...);
if (err == errSomeError) {
Alert("This error occurred.");
}


I am looking for a way to generically obtain the system error message. Similar to how it is done on Windows.


The FormatMessage function can be used to obtain error message strings for the system error codes returned by GetLastError, as shown in the following sample code.



LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL );

MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );

Comments:
Some programs get string from their resources using GetIndString. But those are custom strings as well.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?