Delphi 防止程序重复运行,D11.3/D12调试通过
代码如下红色部分即可
program ProMain;
uses
Vcl.Forms, Windows,SysUtils,
DmPas in 'DmPas.pas' {DM: TDataModule},
uFrmMain in 'uFrmMain.pas' {FrmMain};
{$R *.res}
Var
hMutex:HWND; //把HWND换成Cardinal,因为CreateMutex的API值是Cardinal类型
Ret:Integer;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.Title := 'XXX系统';
hMutex:=CreateMutex(nil,False,'XXX系统');
Ret:=GetLastError;
If Ret = ERROR_ALREADY_EXISTS Then
begin
Application.MessageBox('程序已经运行!','系统',MB_OK);
ReleaseMutex(hMutex);//释放程序
Application.Terminate; //终止执行
end;
Application.CreateForm(TDM, DM);
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end.