我是这样写的。
var myMutex:HWND;begin  myMutex:=CreateMutex(nil,false,'myCopy');// CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字。  if WaitForSingleObject(myMutex,0)<>wait_TimeOut then//程序没有被运行过
但程序打开后自动关闭了。

解决方案 »

  1.   

    CreateMutex(nil,False,'Proj_Plant.exe');
      if GetLastError = ERROR_ALREADY_EXISTS then
      Application.Terminate;
      

  2.   

    'Proj_Plant.exe'是你当前工程的名字
      

  3.   

    在工程文件的
    “ Application.Initialize;”   之前加入如下代码:RvHandle := FindWindow(MYAPPNAME, NIL);
      if RvHandle > 0 then
       begin
         PostMessage(RvHandle, CM_RESTORE, 0, 0);
         Exit;
       end;RvHandle : hWnd;MYAPPNAME为一个常量,在主界面中定义,是你自定义的文件名!在主界面中自定义一个public过程
    procedure CreateParams(var Params: TCreateParams); override;在过程中写入代码:
      inherited CreateParams(Params);
      Params.WinClassName := MYAPPNAME;
      

  4.   

    Application.Initialize;
      application.title:='HighSoftClient';
      tmp:=createmutex(nil,false,'HighSoftClient');
      if getlasterror<>error_already_exists then
      begin
        Application.CreateForm(TMainFrm, MainFrm);
        Application.Run;
      end;
    这样可以使你的程序只运行一次
      

  5.   

    友情提醒: 不可以用 FindWindow。FindWindow是不能完全避免重复运行的。
      

  6.   

    我曾经研究过这个问题,用了好多方法,但是防范不了一种情况——使用的人用每秒超过10次的速率狂点程序。特别是程序在启动电脑后第一次运行,由于加载速度不怎么快,还没有执行到你的代码时,windows已经启动了第二个实例,最终是程序打开了好多个实例。我觉得,最好的控制方法是用汇编调用windows api,建立一个标志,当标志存在时,什么都不做,否则加载正确的程序运行。这已经是最快的了