如何控制我的机器上只有一个project.exe在运行
当第二个打开时提示错误信息。
先谢过

解决方案 »

  1.   

    这种帖子很多了,你搜索一下吧
    用CTEATEMUTEX或者其他很多方法都可以
      

  2.   

    试试下面,可以在工程文件里面添加
    var
    hwnd: THandle;
    begin  
    // form1为主窗体的Caption
      hwnd:= FindWindow(nil, 'form1');
      if hwnd <> 0 then
      begin
        ShowMessage('asd');
        exit;
      end;
      Application.CreateForm(TForm1, Form1);
      Application.CreateForm(TForm2, Form2);
      Application.Run;
    end;
      

  3.   

    // 帮断工程是否已经存在
    if hwnd <> 0 then
      begin
        ShowMessage('提示错误信息');
        exit; // 退出
      end;
      

  4.   

    var
        Mutex:HWND;
    begin
      Application.Initialize;
      Application.Title := 'ISI';
      Mutex:=CreateMutex(nil,False,'main');
      if GetLastError<>ERROR_ALREADY_EXISTS Then
      begin
        Application.CreateForm(Tform1, form1);
        Application.Run;
      end
      else
      begin
        Application.MessageBox('程序已经在运行!','提示框',MB_OK+mb_iconwarning);
        ReleaseMutex(Mutex);
      end;
    end.
      

  5.   

    var
      hPrev: THandle;
      pClass: PChar;
      pClass := PChar('MainForm名称');
      hPrev := FindWindowEx(0, 0 ,pClass, nil);
      IF hPrev <> 0 THEN
        BEGIN
          hPrev := GetWindow(hPrev,GW_HWNDNEXT);
          IF IsIconic(hPrev) THEN
             ShowWindow(hPrev,SW_RESTORE);
          SetForegroundWindow(hPrev);
          Exit;
        END;
    这样就可以了。
    上面是我自己编程中常使用的,绝对可行
    以下帖一些教程关于这方面的  希望对你有帮助
    实现单实例运行的关键是判断前一实例是否存在,Win3.x中运行的程序能获知前一
    实例的句柄,从而可以方便地进行判断,但 Windows 95 是抢先式多任务系统,其程序
    的前一实例句柄恒为零,所以只有另寻其他办法。目前最有效的办法是通过查看是否有
    相同窗口类名的例程存在来进行判断。下面介绍在Delphi中实现的方法。
    1、对主窗口程序的改动:
    在主窗口(即程序创建的第一个窗口)中interface节加入
    const
    CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
    MYAPPNAME = "My Delphi Program";
    并在Form的定义的public节中加入
    procedure CreateParams(var Params: TCreateParams); override;
    Procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
    在implementation节中加入
    {指定窗口名称}
    procedure TForm1.CreateParams(var Params: TCreateParams);
    begin
    inherited CreateParams(Params);
    Params.WinClassName := MYAPPNAME;
    end;{处理“恢复”消息}
    procedure TForm1.RestoreRequest(var message: TMessage);
    begin
    if IsIconic(Application.Handle) = TRUE then
    Application.Restore
    else
    Application.BringToFront;
    end;经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。
    一般在程序刚开始运行的时候进行判断,所以还要对DPR文件进行修改。begin  Application.Initialize;
      Application.Title := 'xxx管理系统;
      hMutex:=CreateMutex(nil,False,'xxx管理系统');
      Ret:=GetLastError;
      If Ret<>ERROR_ALREADY_EXISTS Then
       Begin
       Application.CreateForm(TajMAIN, ajMAIN);  Application.Run;
       End
      Else
       Application.MessageBox('已经运行!','注意!',MB_OK);
       ReleaseMutex(hMutex);end.
    4.
    写注册表:
    procedure WriteReg(input:word);
    var
      tempexe:string;
    begin
      tempexe:=inttostr(input);
    try
      Regtmp:=TRegistry.Create;
      Regtmp.RootKey :=HKEY_LOCAL_MACHINE;
      if Regtmp.OpenKey('\SOFTWARE\Mykey\currentexe',true) then
        begin
          Regtmp.WriteString('curexe',tempexe);
        end;
      Regtmp.Free;
      finally
      //
      end;
    end;
    5.
    //读注册表
    function ReadReg:word;
    var
      str:string;
    begin
      try
      Regtmp:=TRegistry.Create;
      Regtmp.RootKey:=HKEY_LOCAL_MACHINE;
      if Regtmp.OpenKey('\SOFTWARE\Mykey\currentexe',true) then
        begin
          str:= Regtmp.ReadString('curexe');
          result:=strtoint(str);
        end
      else
        result:=0;
      Regtmp.Free;
      finally
      //
      end;
    end;6.
    //工程文件里。
    uses Windows;
    var
      MutexHandle: THandle;
    begin
      MutexHandle := CreateMutex(nil, TRUE, 'Handlename');
      if (MutexHandle<>0) and (GetLastError=ERROR_ALREADY_EXISTS) then
      begin
        CloseHandle(MutexHandle);
        Halt;
      end;
      //..................
      Application.Initialize;
      Application.Run;
    end;
      

  6.   

    个人认为: hanly2008(郁闷人) 的方法可行,有效,好
    呵呵
      

  7.   

    不好意思啊,
    无论用哪种方法加到工程文件里,都提示代码中的一些方法没有定义
    如:[Error] CltXGP.dpr(24): Undeclared identifier: 'GetWindow'
    [Error] CltXGP.dpr(24): Undeclared identifier: 'GW_HWNDNEXT'
    [Error] CltXGP.dpr(26): Undeclared identifier: 'ShowWindow'
    等。
      

  8.   


    在使 用(郁闷人)的代码中出现如下错误:
    [Error] CltXGP.dpr(14): Undeclared identifier: 'HWND'
    [Error] CltXGP.dpr(19): Undeclared identifier: 'CreateMutex'
    [Error] CltXGP.dpr(20): Undeclared identifier: 'ERROR_ALREADY_EXISTS'
    [Error] CltXGP.dpr(30): Undeclared identifier: 'MB_OK'
    [Error] CltXGP.dpr(31): Undeclared identifier: 'ReleaseMutex'
    请问如何解决?
      

  9.   

    下面的代码确保EXE程序只运行一次:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      {搜索数据库看程序是否运行}
      if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
      { 假如没有找到,就添加到数据库}
        atom := GlobalAddAtom('PROGRAM_RUNNING')
      else begin
      { 如果程序已经运行,显示信息并退出程序 }
        MessageDlg('程序已经运行!', mtWarning, [mbOK], 0);
        Halt;
        end;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      { 退出程序时,从数据表中删除添加的条目 }
      GlobalDeleteAtom(atom);
    end;
      

  10.   

    atom是什么意思啊,怎么提示错误啊:
    [Error] LogonUT.pas(110): '(' expected but ':=' found
    说在GlobalAddAtom('PROGRAM_RUNNING')前需要加'('啊