Delphi Basics
GetLastError
Function
Gives the error code of the last failing Windows API call System unit
 function GetLastError : Integer;
Description
The GetLastError function returns the value of the last failed Windows API call.
 
It is useful for many SysUtils routines, such as illustrated in the example.
Related commands
IOResult Holds the return code of the last I/O operation
 
Example code : Try to delete a non-existant file and report the error code
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

begin
  if DeleteFile('CanDeletMe.txt')
  then ShowMessage('File deleted OK')
  else ShowMessage('File not deleted, error code = '+
                   IntToStr(GetLastError));
end;
 
end.
Hide full unit code
   File not deleted, error code = 2
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page