Delphi 防止程序多次运行

program Project1;

uses
  Forms,
  Windows,
  Unit1 in 'Unit1.pas' {Form1};
Var
  hMutex : HWND;
  Ret : Integer;

{$R *.res}

begin
  Application.Initialize;
  Application.Title := 'test';
  hMutex := CreateMutex(nil,false,'test');
  Ret := GetLastError;

  if Ret<>ERROR_ALREADY_EXISTS then
  begin
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end
  else
  begin
    Application.MessageBox('have run','',MB_OK);
    ReleaseMutex(hMutex);
  end;
end.

主要用到的是

CreateMutex 

function CreateMutex(lpMutexAttributes: PSecurityAttributes; bInitialOwner: BOOL; lpName: PChar): THandle;

lpMutexAttributess 是一个  结构类型的指针,可以设置为NULL.

bInitialOwner 是否初始化互斥对象。

lpName 互斥对象的名称。

原文地址:https://www.cnblogs.com/wangmingshuo/p/3323397.html