代码之家  ›  专栏  ›  技术社区  ›  Hadi Eskandari

正在运行的进程的Inno安装程序检查

  •  5
  • Hadi Eskandari  · 技术社区  · 16 年前

    我有一个Inno安装项目,我想在卸载它之前检查应用程序是否正在运行。我尝试了很多方法,但在Windows7中运行时,所有方法都以静默方式失败 notepad.exe psvince.dll 总是回来 false

    我曾经 psvince.dll 在一个C#应用程序中,检查它是否在Windows7下工作,并且工作没有任何问题。所以我最好的猜测是,安装程序无法在启用UAC的情况下正确运行。

    [Code]
    function IsModuleLoaded(modulename: String): Boolean;
    external 'IsModuleLoaded@files:psvince.dll stdcall';
    
    function InitializeSetup(): Boolean;
    begin
       if(Not IsModuleLoaded('ePub.exe')) then
       begin
           MsgBox('Application is not running.', mbInformation, MB_OK);
           Result := true;
       end
       else
       begin
           MsgBox('Application is already running. Close it before uninstalling.', mbInformation, MB_OK);
           Result := false;
       end
    end;
    
    4 回复  |  直到 10 年前
        1
  •  8
  •   mlaan    16 年前

    您正在使用Unicode Inno设置吗?如果你是,它应该说

    function IsModuleLoaded(modulename: AnsiString): Boolean;

    因为psvince.dll不是Unicode dll。

    该示例还检查epub.exe,而不是notepad.exe。

        2
  •  5
  •   Martin Prikryl    6 年前

    您也可以尝试使用WMIService:

    procedure FindApp(const AppName: String);
    var
      WMIService: Variant;
      WbemLocator: Variant;
      WbemObjectSet: Variant;
    begin
      WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
      WbemObjectSet :=
        WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
      if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
      begin
        Log(AppName + ' is up and running');
      end;
    end;
    
        3
  •  4
  •   sashoalm Yaser Kalali    14 年前

    Inno安装程序实际上有一个AppMutex指令,该指令在帮助中有文档记录。实现它需要两行代码。

    在iss文件的[Setup]部分,您可以添加:

    AppMutex=MyProgramsMutexName
    

    然后在应用程序启动期间添加以下代码行:

    CreateMutex(NULL, FALSE, "MyProgramsMutexName");
    
        4
  •  1
  •   TLama    11 年前

    [Code]
    function IsAppRunning(const FileName: string): Boolean;
    var
      FWMIService: Variant;
      FSWbemLocator: Variant;
      FWbemObjectSet: Variant;
    begin
      Result := false;
      FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');
      FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
      FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));
      Result := (FWbemObjectSet.Count > 0);
      FWbemObjectSet := Unassigned;
      FWMIService := Unassigned;
      FSWbemLocator := Unassigned;
    end;
    
    function InitializeSetup: boolean;
    begin
      Result := not IsAppRunning('notepad.exe');
      if not Result then
      MsgBox('notepad.exe is running. Please close the application before running the installer ', mbError, MB_OK);
    end;
    
    推荐文章