GetLastError: how does it work with DECLARE DLL ?


Many Windows APIs can fail for various reasons. For example FormatMessage indicates failure by returning 0. (see GetLastError is a number. What does it mean?) To get further information about the failure, call the GetLastErrror API, which queries the value set by the last SetLastError call.  One can imagine that these APIs call SetLastError to initialize the Last Error value to 0. If an error occurs, this value is changed appropriately.


 


The problem is that the Last Error is a global value, being set and reset by Windows API calls. Behind the scenes FoxPro calls the Win API for many services such as memory management, I/O, etc. Also, Windows sends Fox messages for many services (such as paint the window!) which cause Fox to call Windows API functions. Also, APIs can call other APIs. That means between executing lines of your fox code, many Windows API calls could have been called, possibly changing the Last Error value. Thus the GetLastError truly does get the “Last Error”, but there may have been many intervening calls after the call your code makes that failed.


 


I changed VFP9 so that this global value is isolated to your code. If you call GetLastError and SetLastError, they will reflect the last DLL call, regardless of intervening API calls.


 


VBA and VB.Net both have a LastDLLError property to achieve the same goal.


 

Comments (6)

  1. Kevin Wright says:

    > I changed VFP9 so that this global value is isolated to your code.

    Is this in the released VFP9, or coming in SP1?

    > VBA and VB.Net both have a LastDLLError property to achieve the same goal.

    How about C# ?

    Kevin

  2. Calvin,

    Is there anyway to subscribe (RSS) to comments on your blog? Your default RSS feeds cover only your posts.

    I enjoy your blog very much,

    Malcolm

  3. CalvinH says:

    Kevin: it was in the released VFP9. Sorry I wasn’t clear.

    Malcolm: I’m glad you enjoy my blog. I believe you can subscribe (RSS) via the "Email" link under "My Links" on this page: this requires that you "Sign in" or "Join" from http://blogs.msdn.com. That link shows a page with an option to subscribe to “posts and articles” or “posts, articles and comments”

  4. Malcolm Greene says:

    Calvin,

    I followed your advice, joined blogs.msdn.com and saw the option to be emailed (vs. RSS fed) info on posts, articles, and comments. I think(?) I signed up for this option, but the blogs.msdn.com site provides NO feedback on whether a signup request was processed or not – it just stays on the same page. If this option works, I’ll post back a confirmation.

    If anyone else has experience enabling RSS feeds with comments, please share it with this list.

    Thanks again,

    Malcolm

  5. Rodd Harris says:

    Calvin,

    I was sent here by someone from UT regarding a problem I’m having with GetLastError API call.  It always returns 0 even if I enter a bogus password to LogonUser().

    Base on what I read above, are you saying that GetLastError will never properly work in VFP 8.0?

    Thanks for your help.

  6. For this post: GetLastError: how does it work with DECLARE DLL ?, I received this question:

     

Skip to main content