Reset GetLastError value
Delphi Xe.
in delphi help: "...Calling this function usually resets the operating system error state..."
How to res开发者_如何学Pythonet a current error on 0? I.e. that GetLastError=0
Example:
Try
// There is an error
except
showmessage(inttostr(getlasterror)); // Ok, getlasterror<>0
end;
....
// no errors
....
// How to reset a current error on 0?
showmessage(inttostr(getlasterror)); // Again will be <> 0
You should only be calling GetLastError
when there has actually been an error. Some Windows API functions will reset the error to 0 on success, some won't. Either way, you should only interrogate the error state when you need to know the most recent error.
Note that there is a SetLastError
method as well, but that doesn't help you; if you set the last error to 0, then of course GetLastError
will return 0.
It's actually a Win32 call (not Delphi per se).
You can clear it with "SetLastError ()".
Here's the MSDN documentation:
http://msdn.microsoft.com/en-us/library/ms679360%28v=vs.85%29.aspx
This is an example of low quality documentation. GetLastError
WinAPI function will retain its return value until next call of SetLastError
so repeatedly invoking will have no effect.
SetLastError(42);
for I := 1 to 100 do
Assert(GetLastError() = 42); // all of those assertions evaluates to True
Also, in Delphi documentation GetLastError has been misplaced into exception handling routines; this is wrong too, these error handling mechanisms are unrelated to each other.
On that silly "usually" word in the reference: It happens because function used to output GetLastError return value invokes SetLastError. Eg:
SetLastError(42);
OutputDebugString(PChar(Format('GetLastError() = %d', [GetLastError()]))); // 42
OutputDebugString(PChar(Format('GetLastError() = %d', [GetLastError()]))); // 0! (ERROR_SUCCESS set by the previous OutputDebugString call)
精彩评论