问题就是:当我启动了一个Delphi的exe后,当此exe还没关闭之前,当再次启动同一个exe时,出提示信息,并且不让此exe启动!还望大家多多指教,谢谢!

解决方案 »

  1.   

    MutexNotFirst := createMutex(nil,False,Mutex2);
        if (GetLastError() =ERROR_ALREADY_EXISTS) then  // a second one is running
             halt;
      

  2.   

    能帮忙解释一下吗?另外这段程序写在FormCreate中吗?
      

  3.   

    createMutex是向Windows注册一个全局的命名对象.
    第一次调用时注册成功, GetLastError = 0;
    第二次调用时注册同名时, GetLastError = ERROR_ALREADY_EXISTS;
    是在FormCreate中写.
      

  4.   

    那么MutexNotFirst是什么类型啊!谢谢!
      

  5.   

    {$R *.RES}
    var
      MutexHandle:THandle;begin
      MutexHandle:=windows.CreateMutex(nil,true,'你的窗体名如:Form1');
      if mutexHandle<>0 then
      begin
        if GetLastError=windows.ERROR_ALREADY_EXISTS then
        begin
          closeHandle(Mutexhandle);  //关闭互斥区
          halt;  //终止本程序的运行
        end;
      end;
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      showwindow(application.handle, SW_HIDE);    {隐藏句柄}
      Application.ShowMainForm:=false;             {隐藏窗体}
      Application.Run;
    end.
      

  6.   

    sundayyoung(sundayyoung)你的这段代码一写,连第一次运行都不行了,是不是写在FormCreate中啊?
      

  7.   

    哈哈,你查查看你的project中哪里有   {$R *.RES}   ,你就把程序放在下面好了(注意要把原有的覆盖掉)