Delphi Xe.
在delphi帮助中:"...Calling此函数通常重置操作系统错误状态...“
如何在0上重置当前错误?即GetLastError=0
示例:
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发布于 2011-07-03 01:45:12
您应该只在实际发生错误时调用GetLastError。有些Windows API函数会在成功时将错误重置为0,有些则不会。无论哪种方式,您都应该仅在需要了解最新错误时才询问错误状态。
注意,还有一个SetLastError方法,但这对您没有帮助;如果您将最后一个错误设置为0,那么GetLastError当然会返回0。
发布于 2011-07-03 01:42:56
这实际上是一个Win32调用(不是Delphi本身)。
你可以用"SetLastError ()“清除它。
以下是MSDN文档:
http://msdn.microsoft.com/en-us/library/ms679360%28v=vs.85%29.aspx
发布于 2011-07-03 03:55:58
这是一个低质量文档的例子。GetLastError WinAPI函数将保留其返回值,直到下一次调用SetLastError,因此重复调用将不起作用。
SetLastError(42);
for I := 1 to 100 do
Assert(GetLastError() = 42); // all of those assertions evaluates to True此外,在Delphi文档中,GetLastError被错误地放在异常处理例程中;这也是错误的,这些错误处理机制彼此无关。
在引用中那个愚蠢的“通常”字上:这是因为用于输出GetLastError返回值的函数调用了SetLastError。例如:
SetLastError(42);
OutputDebugString(PChar(Format('GetLastError() = %d', [GetLastError()]))); // 42
OutputDebugString(PChar(Format('GetLastError() = %d', [GetLastError()]))); // 0! (ERROR_SUCCESS set by the previous OutputDebugString call)https://stackoverflow.com/questions/6558578
复制相似问题