function OpenMutex( dwDesiredAccess: DWORD; {打开权限} bInheritHandle: BOOL; {能否被当前程序创建的进程继承} pName: PWideChar {Mutex 对象的名称}): THandle; stdcall; {成功返回 Mutex 的句柄; 失败返回 0}注意, 这里的 CreateMutex 函数应该有个名了, 因为 OpenMutex 要用到; 另外, CreateMutex 的第二个参数已经不重要了(也就是 True 和 False 都行), 因为这里是用其名称来判断的. 程序可以这样写:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}var hMutex: THandle;const NameMutex = 'MyMutex';procedure TForm1.FormCreate(Sender: TObject);begin if OpenMutex(MUTEX_ALL_ACCESS, False, NameMutex) <> 0 then begin ShowMessage('该程序已启动'); Application.Terminate; end; hMutex := CreateMutex(nil, False, NameMutex);end;procedure TForm1.FormDestroy(Sender: TObject);begin CloseHandle(hMutex);end;end.这一般都是写在 dpr 主程序里, 省得让后启动的程序执行些无用的代码:
program Project1;uses Forms, Windows, Unit1 in 'Unit1.pas' {Form1};{$R *.res}var hMutex: THandle;const NameMutex = 'MyMutex';begin {主线程入口} if OpenMutex(MUTEX_ALL_ACCESS, False, NameMutex) <> 0 then begin MessageBox(0, '该程序已启动', '提示', MB_OK); Application.Terminate; end; hMutex := CreateMutex(nil, False, NameMutex); Application.Initialize; Application.MainFormOnTaskbar := True; Application.CreateForm(TForm1, Form1); Application.Run; CloseHandle(hMutex); {主线程出口}end.