Delphi防止程序多次运行
Delphi防止程序多次运行
program MyProject;
uses
Forms,Windows,SysUtils,
//加上Windows,SysUtils是让API函数生效
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
Var
hMutex:HWND; //把HWND换成Cardinal,因为CreateMutex的API值是Cardinal类型
Ret:Integer;
begin
Application.Initialize;
Application.Title := '明日科技';
hMutex:=CreateMutex(nil,False,'明日科技');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then
Begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end
Else
begin
Application.MessageBox('程序已经运行!','明日科技',MB_OK);
ReleaseMutex(hMutex);//释放程序
end;
end.
浙公网安备 33010602011771号