WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: DAG_P6 on September 15, 2013, 01:23:29 AM

Title: LastDLLError
Post by: DAG_P6 on September 15, 2013, 01:23:29 AM
Context

I have developed a small set of WIL UDFs that implement wrappers around the Win32 mutex functions, a couple of which return useful diagnostic information through SetLastError.

Question

I have discovered, the hard way, that the Win32 GetLastError() function must be called right away, because the very next DLL call may trample it. My concern is that an intervening Windows API call, made on behalf of the WIL interpreter, over which I have no control, might trample the last error set by a DLLCall before a subsequent call, also via DllCall, to GetLastError, can retrieve it.

Is it safe to assume that the value returned by the native LastDLLError function is the result of a Windows API call to GetLastError that happens as part of the code that implements the WIL DLLCall function?
Title: Re: LastDLLError
Post by: kdmoyers on September 16, 2013, 04:17:38 AM
((( "a small set of WIL UDFs that implement wrappers around the Win32 mutex functions"  Sounds cool! )))
Title: Re: LastDLLError
Post by: td on September 16, 2013, 06:55:53 AM
Quote from: DAG_P6 on September 15, 2013, 01:23:29 AM

...

I have discovered, the hard way, that the Win32 GetLastError() function must be called right away, because the very next DLL call may trample it. My concern is that an intervening Windows API call, made on behalf of the WIL interpreter, over which I have no control, might trample the last error set by a DLLCall before a subsequent call, also via DllCall, to GetLastError, can retrieve it.

Is it safe to assume that the value returned by the native LastDLLError function is the result of a Windows API call to GetLastError that happens as part of the code that implements the WIL DLLCall function?

The value returned by LastDLLError is the system error state immediately after an assembly mov instruction has been issued to record the current stack pointer after the call to the dll entry point.  The saved stack pointer is then used for further error checking. 
Title: Re: LastDLLError
Post by: DAG_P6 on September 16, 2013, 04:58:00 PM
Thanks again, Tony. That was the answer I hoped (and expected) to receive.

I'll treat LastDllError as I would the value returned by the call to GetLastError that immediately follows any call to a routine that reports through SetLastError when I am coding in C or C++.