码迷,mamicode.com
首页 > Windows程序 > 详细

让 Delphi 程序只运行一个副本

时间:2015-01-27 09:36:30      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

program mutex;

uses
  Windows,
  Forms,
  Unit1 in ‘Unit1.pas‘ {Form1};

{$R *.res}

const
  MutexStr = ‘4C3201D5-6A5A-4B8B-A2F0-B103985705F4‘; // GUID

var
  hAppMutex: THandle;

begin
  ReportMemoryLeaksOnShutdown := DebugHook = 1;
  Application.Initialize;
  hAppMutex := CreateMutex(nil, False, PChar(MutexStr));
  if (hAppMutex = 0) then begin
   Application.MessageBox(‘创建互斥对象失败!‘, ‘错误提示‘, MB_ICONSTOP);
   exit;
  end;
  if ((hAppMutex <> 0) and (GetLastError() = ERROR_ALREADY_EXISTS)) then begin
   Application.MessageBox(‘本程序只能运行一个副本!‘, ‘错误提示‘, MB_ICONSTOP);
   CloseHandle(hAppMutex);
   exit;
  end;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
  CloseHandle(hAppMutex);
end.

让 Delphi 程序只运行一个副本

标签:

原文地址:http://my.oschina.net/u/582827/blog/372024

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!