在DELPHI的工程文件中
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Windows,Messages,ShellApi;
{$R *.res}
var
HMutex:Hwnd;
Ret:Integer;
Reg :integer;
begin
Application.Initialize;
Application.Title :='这是一个防止多个实例运行的程序';
HMutex :=CreateMutex(nil,False,Pchar('这是一个防止多个实例运行的程序'));
Reg :=GetLastError;
if Reg<>ERROR_ALREADY_EXISTS then
begin
Application.CreateForm(TForm1, Form1);
end
else
begin
MessageBox(0,'实例已经运行了!','错误', MB_OK + MB_ICONERROR);
ReleaseMutex(hMutex);
end;
Application.Run;
end.