delphi - Why does GetLastError have an error before my program begins? -


while using getlasterror after calling windows api function wrapper extractshortpathname noticed getlasterror returns non-zero error code regardless of whether call extractshortpathname succeeded or failed. in fact, there seems "last error" before program executes, e.g.

program testgetlasterror;  {$apptype console} {$r *.res}  uses   system.sysutils;  var   errorcode: integer;  begin   try     errorcode := getlasterror;     if errorcode <> 0       raiselastoserror;   except     on e: exception       writeln(e.classname, ': ', e.message);   end; end. 

results in:

eoserror: system error.  code: 122. data area passed system call small 

am misunderstanding or doing wrong?

if delphi run-time doing results in getlasterror being set, correct way clear error before program starts executing? should use setlasterror(error_success); example delphi api documentation:

procedure tform2.btraiselastclick(sender: tobject); begin   { set last os error bogus value. }   system.setlasterror(error_access_denied);    try     raiselastoserror();   except     on ex : eoserror       messagedlg('caught os error code: ' + inttostr(ex.errorcode), mterror, [mbok], 0);   end;    { let delphi exception dialog appear. }   raiselastoserror(error_not_enough_memory);    { set last error none. }   system.setlasterror(error_success);    if getlasterror() <> error_success     messagedlg('whoops, went wrong in mean time!', mterror, [mbok], 0);    { no exception should thrown here because last os error "error_success". }   checkoserror(getlasterror()); end; 

http://docwiki.embarcadero.com/codeexamples/tokyo/en/lastoserror_(delphi)

getlasterror's documentation indicates of use if

  • an api call fails, ,
  • the function fails indicates can use getlasterror more information why. documentation (emphasis mine)

the return value calling thread's last-error code.

the return value section of documentation each function sets last-error code notes conditions under function sets last-error code. functions set thread's last-error code set when fail. however, functions set last-error code when succeed. if function not documented set last-error code, value returned function recent last-error code have been set; functions set last-error code 0 on success , others not.

this indicates calling without first having failure in function documented set on failure meaningless. can't call getlasterror unless know error occurred, , if you're calling after specific function called indicates failed.


Comments