GetLastError在调用Windows API函数包装器之后使用,就像ExtractShortPathName我注意到的那样GetLastError,无论调用ExtractShortPathName成功还是失败,都会返回非零错误代码.事实上,在我的程序执行之前似乎存在"最后一个错误",例如
program TestGetLastError;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
ErrorCode: Integer;
begin
try
ErrorCode := GetLastError;
if ErrorCode <> 0 then
RaiseLastOSError;
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
end.
Run Code Online (Sandbox Code Playgroud)
结果是:
EOSError: System Error. Code: 122.
The data area passed to a system call is too small
Run Code Online (Sandbox Code Playgroud)
我误解了什么或做错了什么?
如果Delphi运行时正在执行某些导致GetLastError设置的操作,那么在程序开始执行之前清除该错误的正确方法是什么?我应该SetLastError(ERROR_SUCCESS);像Delphi API文档中的这个示例一样使用:
procedure TForm2.btRaiseLastClick(Sender: TObject);
begin
{ Set the last OS error to a bogus value. …Run Code Online (Sandbox Code Playgroud)