When Win32 API functions fail it is possible to learn more about the cause of the error by calling GetLastError(). GetLastError() returns an error number that is set by the API by calling SetLastError(). It is important to read the documentation before using GetLastError() to ensure that the API actually sets a error, if it doesn’t GetLastError() will happily return an error number set by a different function call.
Of course the next step is to figure out what that number means. It is important to use %d when formatting the error number because most of the ways that you can use to look up the error use base 10. Some ways of getting a human readable message are:
· eMbedded Visual C++ and Visual Studio have an Error Lookup dialog in the Tools menu.
· Check winerror.h for the error code, this is useful if you are using an older version of Platform Builder. Winerror.h is in Public\Common\SDK\Inc.
· Use FormatMessage() to output a human readable message to your user. The up side of this is that it easy to get a message, the downside is that the FormatMessage() API is an optional component so not all Windows CE systems will include it. Here is a function that uses FormatMessage() and handles the case where FormatMessage() isn’t in the OS:
void OutputError(TCHAR *Function, TCHAR *Message)
TCHAR ErrorString[ 30 ];
DWORD Error = GetLastError();
0, // Default language
) == 0 )
// FormatMessage failed, which is most likely becuase
// it isn't supported on this platform
swprintf( ErrorString, TEXT("Unknown Error (%d)"), Error );
ErrorMessage = ErrorString;
lpMsgBuf = NULL;
ErrorMessage = lpMsgBuf;
RETAILMSG( 1, (TEXT("%s %s %s\n"), Function, Message, ErrorMessage ));
if( lpMsgBuf )
LocalFree( lpMsgBuf );
Copyright © 2008 – Bruce Eitman
All Rights Reserved