检查应用程序是否正在运行

时间:2010-09-03 05:54:37

标签: .net inno-setup

我正在使用innosetup为我的Windows应用程序创建安装。在开始安装之前,我需要检查应用程序是否已经运行。我使用了以下代码,但无法正常工作。

const
WM_CLOSE = 16;

Function InitializeSetup : Boolean;
var winHwnd: longint;
    retVal : boolean;
    strProg: string;
begin
  Result := true;
  try
    strProg := 'myApp.exe';
    winHwnd := FindWindowByWindowName(strProg);
    Log('winHwnd: ' + inttostr(winHwnd));
    if winHwnd <> 0 then
      retVal:=postmessage(winHwnd,WM_CLOSE,0,0);
      if retVal then begin
        MsgBox('Window is  not running', mbInformation, MB_OK);
        Result := True
        end
      else begin
       MsgBox('Window still open', mbInformation, MB_OK);
        Result := False;
 end;
  except
  end;

end;

这里winHwnd总是为0.请让我知道这段代码有什么问题。

谢谢, 馒头

1 个答案:

答案 0 :(得分:1)

InnoSetup有一个内置检查以查看您的应用程序是否正在运行,请查看[Setup]部分中的AppMutex设置。您需要做的就是在应用程序中创建一个命名的互斥锁,并在innosetup脚本中指定该互斥锁的名称。然后InnoSetup将进行检查并显示一条消息。