📄 getlasterrorexampleu.pas
字号:
unit GetLastErrorExampleU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
ExecInfo: TShellExecuteInfo; // required for ShellExecuteEx
ErrorMessage: Pointer; // a pointer to the error message text
ErrorCode: DWORD; // holds the last error code
begin
{prepare the data structure for the ShellExecuteEx function. this
function will attempt to open a non existant file. this causes
an error code to be set.}
ExecInfo.cbSize := SizeOf(TShellExecuteInfo);
ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
ExecInfo.Wnd := Form1.Handle;
ExecInfo.lpVerb := 'open';
ExecInfo.lpFile := 'c:\I_Do_Not_Exist.exe';
ExecInfo.lpParameters := '';
ExecInfo.lpDirectory := '';
ExecInfo.nShow := SW_SHOWNORMAL;
{attempt to open and launch the non existant file}
ShellExecuteEx(@ExecInfo);
{get the last error code for the calling thread}
ErrorCode := GetLastError;
{retrieve the string describing this error code}
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,
nil, ErrorCode, 0, @ErrorMessage, 0, nil);
{display the value of the last error code and its associated description}
MessageDlg('GetLastError result: '+IntToStr(ErrorCode)+#13+
'Error Description: '+string(PChar(ErrorMessage)),
mtError, [mbOk], 0);
{Windows allocated the memory for the description string,
so we must free it.}
LocalFree(hlocal(ErrorMessage));
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -