Mutex 作为系统核心对象是可以跨进程的(临界区就不行), 我们可以利用互斥对象禁止程序重复启动.

工作思路:
先用 OpenMutex 尝试打开一个自定义名称的 Mutex 对象, 如果打开失败说明之前没有这个对象存在;
如果之前没有这个对象, 马上用 CreateMutex 建立一个, 此时的程序应该是第一次启动;
再重复启动时, 那个 OpenMutex 就有结果了, 然后强制退出.
最后在程序结束时用 CloseHandle 释放 Mutex 对象.

function OpenMutex(
  dwDesiredAccess: DWORD; {打开权限}
  bInheritHandle: BOOL;   {能否被当前程序创建的进程继承}
  pName: PWideChar        {Mutex 对象的名称}
): THandle; stdcall;      {成功返回 Mutex 的句柄; 失败返回 0}


注意, 这里的 CreateMutex 函数应该有个名了, 因为 OpenMutex 要用到;
另外, CreateMutex 的第二个参数已经不重要了(也就是 True 和 False 都行), 因为这里是用其名称来判断的.

程序可以这样写:
unit Unit1;

interface

uses
  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.

posted on 2009-02-13 23:06  万一  阅读(9463)  评论(11编辑  收藏  举报