有个a.exe程序已经执行,如果在打开a.exe程序时,由于已经存在了,所以希望第二次打开a.exe不执行了,请问如何处理?
  我自己写了一个可是不行,请指教!
  program a;uses
  Forms,
  SysUtils,
  Windows,
  Mainfromunit in 'Mainfromunit.pas' {MainForm},
  
{$R *.res}
var
  RvHandle: hWnd;begin
  RvHandle := FindWindow('a', nil);
  if RvHandle > 0 then Exit;
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

解决方案 »

  1.   

    Process[] ps=Process.xxxx  (xxxx忘了,用于获得所有进程)
    foreach(Process p in ps)
    {
        遍历,寻找有没有a程序
    }
      

  2.   

    procedure TForm1.FormCreate(Sender: TObject);
    var ZAppName:array[0..127] of char;
       Hold:string;
       Found:HWND;
    begin
      Hold:=Application.Title;
      Application.Title:='OnlyOne'+IntToStr(HInstance);//暂时修改窗口标题
      StrPCopy(ZAppName,Hold);   //原窗口标题
      Found:=FindWindow(nil,ZappName); //查找窗口
      Application.Title:=Hold;
      if Found<>0 then    //找到则激活已运行的程序并结束自身
        begin
          ShowWindow(Found,SW_RESTORE);
          Application.Terminate;
        end;
    end;
      

  3.   

    修改你的工程文件为这个样子,这样既保证你的程序不被运行多个,同时,当再次运行时,原先运行的还会得到焦点program Project1;uses
      Forms,SysUtils,Windows,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}
    var
      FindHID: HWND;
      MoudleName: string;  function EnumWndProc(hWnd:THandle; Param:Cardinal): Boolean; stdcall;
      var
        ClassName, WinMoudleName:string;
        WinInstance: THandle;
      begin
        Result := true;
        SetLength(ClassName, 1000);
        GetClassName(hWnd, pChar(ClassName), Length(ClassName));//获得当前遍历窗口的类名
        ClassName := pChar(ClassName);//在字符串后加结束符,确定字符串结束
        if ClassName = TForm1.ClassName then//比较
        begin
          WinInstance := GetWindowLong(hWnd, GWL_HINSTANCE);//获得当前遍历窗口的实例
          SetLength(WinMoudleName, 1000);
          GetModuleFileName(WinInstance, pChar(WinMoudleName), Length(WinMoudleName)); //获得当前遍历窗口的程序文件名
          WinMoudleName := pChar(WinMoudleName);
          if WinMoudleName = MoudleName then//MoudleName为工程全局变量,自身程序的文件名
          begin
            FindHid := hWnd;//FindHid为工程全局变量保存找到的句炳
            Result := False;//找到以后就结束遍历
          end;
        end;
      end;
      function OneInstanceOnly(MutexName: string): Boolean;
      var
        Mutex: HWND;
      begin
        Result := True;
        Mutex := CreateMutex(nil, False, PAnsiChar(MutexName)); //CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字。
        if WaitForSingleObject(Mutex, 0) = WAIT_TIMEOUT then//程序已在运行
          Result := False;
      end;
    begin
      if not OneInstanceOnly('你的程序名,最好取名唯一点') then
      begin
        SetLength(MoudleName,1000);
        GetModuleFileName(HInstance, pChar(MoudleName), Length(MoudleName)); //获得自己程序文件名
        MoudleName := pChar(MoudleName); //在字符串后加结束符,确定字符串结束
        EnumWindows(@EnumWndProc, 0); //调用枚举函数, @EnumWndProc 是处理找到窗口的回调函数。
        if FindHid <> 0 then
          SetForegroundWindow(FindHid);
        Exit;
      end;  Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
      

  4.   

    //介个问题前段时间刚问过
    program Project1;uses
      Forms,
      Windows,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}
    var
        hMutex:hWnd;
    begin
      Application.Initialize;
      Application.Title:='test';
      hMutex:=CreateMutex(nil,false,'test');
      if GetLastError<>Error_Already_Exists then
      begin
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      end
      else
      begin
        Application.MessageBox('本程序只允许同时运行一个','Error');
        ReleaseMutex(hMutex);
      end;
    end.