Using Windows GetLastError with Native Library Calls

1/16/2009 11:20 PM
You can subscribe to this wiki article using an RSS feed reader.

You can use the Windows GetLastError function to access the results of native library calls to other Windows APIs.

You must use StyleTap native library support to access the GetLastError function, the same way you access any other native library entry points.

In order for the result of calling GetLastError to be useful, you must call it IMMEDIATELY after you call the Windows function that GetLastError pertains to. If you call any other Windows function, or if you do anything else (which might make a Windows call that you aren't aware of), the result from GetLastError will not apply to the function call that you want to find out about.

Even calling your StyleTap GetLastError wrapper function the first time has some Windows side effects, as it loads the native dll (coredll) and finds and caches the proc address for GetLastError. Because of these side effects, you must call GetLastError once before you really need to use it. The first time that you call GetLastError, it will preload the cached information. After that, when you call GetLastError you will get the results of the last Windows call before GetLastError was called.

The attached sample program illustrates the use of GetLastError and the recommended wrapper for it.