Geeks With Blogs
Windows Embedded Annotations

Very often I read posts in the newsgroups like:

I call function Foo(dwParam) and it returns FALSE. What's wrong?

If I cannot tell it immediately -for example because the dwParam is clearly wrong - my standard answer is:

Have you called GetLastError()? Which error value it returns?

GetLastError (if the call that fails actually sets an error and the error is meaningful) can be very helpful to detect why the call is failing. If you do not understand exactly what the code means and you have the code you may figure out where and why the failing function is calling SetLastError with that specific code.

Take in account that the error value you get is per thread -the kernel structure modeling the thread has a DWORD member named dwLastError: Since GetLastError retrieves the latest error this means that, if the failing function you are investigating does not uses SetLastError(), the error you get is related to a previous call to another function so can be misleading (or it can be 0 since a function you call before succeded and set the error to ERROR_SUCCESS.

The error codes are defined in the documentation but, for a more complete list and meaning you can check  %_WINCEROOT%\PUBLIC\COMMON\SDK\INC\winerror.h.

When you write your own code is a good practice to use SetLastError(dwError) with a meaningful error value; if you do not find an adapted system value for the error you can define your own: an error code which has bit 29 set  is guaranteed not to conflict with any system error code.

The error codes are DWORD's which should be formatted as follows - according to winerror.h:

Bits 31 & 30 are the severity code where

          00 - Success
          01 - Informational
          10 - Warning
          11 - Error

Bit 29 is the Customer code flag

Bit 28 is a reserved bit

Bits 28-16 are the facility code,  which -more or less- identifies the subsytem generating the error, Internet, Storage, etc

Bits 15-0 are the actual error code


Take in account that the severity code is almost not used in winerror.h codes: for example ERROR_INVALID_PARAMETER which is undoubtly an error (neither a warning nor an information) has a value of 0x57L, not 0xC0000057L.

On the contrary the severity bits are used in Windows NT-based OS NTSTATUS error codes: for example STATUS_UNSUCCESSFUL which is the more generic error that you will have has a value of 0xC0000001L.

By the way, most DDK functions returns an NTSTATUS code so there's no need to call a function like GetLastError to have more information about the error itself.

Posted on Wednesday, November 4, 2009 10:41 AM Windows CE | Back to top

Comments on this post: Get last error and set it meaningfully

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Luca Calligaris | Powered by: