var  
  ret:integer;
if ret = Error_ALREADY_EXISTS then
  Application.MessageBox('系统已在运行!','提示',mb_OK);

解决方案 »

  1.   

    JonHua(玉龙) ,你的代码不行啊:提示Error_ALREADY_EXISTS为未定义的变量,能否再帮忙看看。
      

  2.   

    program AUTORUN;uses
      Forms,Windows,Messages,
      FRM_AUTORUN in 'FRM_AUTORUN.pas' {LOGO};
      const// 自定义一个消息,用来恢复窗口
      CM_RESTORE=WM_USER+$1000;
      APPNAME='安装程序';
      VAR RvHandle:hWnd;
      {$R *.RES}
    begin
    RvHandle :=FindWindow(APPNAME, NIL); // 根据窗体类名查找窗口句柄
    if RvHandle > 0 then // 如果找到则发送自定义的消息并退出
    begin
         PostMessage(RvHandle,CM_RESTORE,0,0);
         SetForegroundWindow(RvHandle);
         exit;
    end ;
      Application.Initialize;
      Application.Title := '网络版安装引导界面';
      Application.CreateForm(TLOGO, LOGO);
      Application.Run;
    end.
    /////////////////////////////////////////////////////unit FRM_AUTORUN;interfaceuses
        Windows,Forms,jpeg,Controls, StdCtrls, ExtCtrls, Classes,Messages,
        shellapi, SysUtils;
    const// 自定义一个消息,用来恢复窗口
      CM_RESTORE=WM_USER+$1000;
      APPNAME='安装程序';
    type
      TLOGO = class(TForm)
      private
        { Private declarations }  public
        { Public declarations }
        procedure CreateParams(var Params: TCreateParams); override;
        Procedure RestoreRequest(var message: TMessage); message CM_RESTORE ;
      end;var
      LOGO: TLOGO;implementation{$R *.DFM}
    procedure Tlogo.CreateParams(var Params: TCreateParams);
    begin
    // 设置窗体的类名
    inherited CreateParams(Params);
    Params.WinClassName := APPNAME;
    end ;procedure Tlogo.RestoreRequest(var message: TMessage);
    begin
    // 接到自定义的消息后,如果处于最小化状态则恢复,否则放置到桌面的最前面
    //messageBox (Handle , '程序" ' + APPNAME+ ' "已经运行了。' ,'信息' , MB_OK + MB_ICONINFORMATION + MB_SYSTEMMODAL) ;
    if IsIconic(Application.Handle) = TRUE then
    Application.Restore
    else
    Application.BringToFront;
    end;
    end.
    程序运行时会进行“根据窗体类名查找窗口句柄”操作,找到了将那个程序
    设为前台,并关闭自已,
    如没找到,则继续运行。也就是实现一个程序只能运行 一个实例,
      

  3.   

    把这段程序加到 project-->view source 
     var
                                                //避免程序的二次运行;
      h: HWND;
      begin
      Application.Initialize;
      h := FindWindowEx(0, 0, 'TApplication', '管理系统');
      if h <> 0 then Application.Terminate;
      Application.Title := '管理系统';    Application.Title := '管理系统';
        Application.CreateForm(TMianForm, MianForm);
       LoginForm.Free;
        Application.Run;
      

  4.   

    var Mutex: THandle; 
    begin 
     Mutex := CreateMutex(nil, True, 'SingleApp'); //SingleApp,一个比较特殊的名字
     if GetLastError <> ERROR_ALREADY_EXISTS then 
     begin 
       Application.Initialize; 
     end;
      

  5.   

    if FindWindow('TApplication', '****系统') <> 0 then Halt;
      

  6.   

    var hMutex: THandle;
    begin
      hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
      if hMutex <> 0 then Exit;
      hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
      if hMutex = 0 then Exit;
      try
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      finally
        ReleaseMutex(hMutex);
      end;
    end.
      

  7.   

    创建互坼体var hMutex: THandle;
    begin
      hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
      if hMutex <> 0 then Exit;
      hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
      if hMutex = 0 then Exit;
      try
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      finally
        ReleaseMutex(hMutex);
      end;
    end.
      

  8.   

    创建互坼体var hMutex: THandle;
    begin
      hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
      if hMutex <> 0 then Exit;
      hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
      if hMutex = 0 then Exit;
      try
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      finally
        ReleaseMutex(hMutex);
      end;
    end.
      

  9.   

    刚才有个字符串没有改掉。
    var hMutex: THandle;
    begin
      hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
      if hMutex <> 0 then Exit;
      hMutex := CreateMutex(nil, False, 'YourString');
      if hMutex = 0 then Exit;
      try
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      finally
        ReleaseMutex(hMutex);
      end;
    end.
      

  10.   

    方法比较多,除上面诸大侠给的FINDWINDOW,创建互斥体外, 还可以使用全局元子变量,设置内存变量,建立临时文件作为运行标志等。