Get last error and set it meaningfully


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.

Advertisements
This entry was posted in Windows Embedded CE and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s