【问题标题】:How to uninstall the program when it is running and only after the user's confirmation to close and uninstall it - Inno Setup如何在程序运行时卸载程序,并且只有在用户确认关闭和卸载它之后才能卸载它 - Inno Setup
【发布时间】:2016-11-02 15:16:42
【问题描述】:

嘿,当我的程序在 Windows 中运行时,我需要使用 Inno Setup 卸载它。

我喜欢我的卸载程序检测它是否正在运行并向用户提供消息框,例如 “Windows 10 Logon Background Changer 正在运行。您要关闭它并卸载吗?” p>

上面描述的消息框应该有两个按钮(YesNo)。

当这个消息框出现在用户选择卸载它后,即使它正在运行,当用户在消息框中选择时,卸载程序应该关闭。

如果用户在该消息框中选择,卸载程序应该终止正在运行的程序进程并正常卸载它(不是静默)。

我为此编写了一个代码,但它失败了并且给出了错误的行为。

我的代码是:

function IsProcessRunning(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 InitializeUninstall(): Boolean;
  var ErrorCode: Integer;
begin
  Result := not IsProcessRunning('W10logbcr.exe');
  if not Result then
  Msgbox('Windows 10 Logon Background Changer is running.Do you like to close it automatically and uninstall?', mbError, MB_YESNO);
  if Msgbox('Windows 10 Logon Background Changer is running.Do you like to close it automatically and uninstall?', mbError, MB_YESNO) = IDNO then exit;
  if Msgbox('Windows 10 Logon Background Changer is running.Do you like to close it automatically and uninstall?', mbError, MB_YESNO) = IDYES then begin
    ShellExec('open','taskkill.exe','/f /im W10logbcr.exe','',SW_HIDE,ewNoWait,ErrorCode);
    ShellExec('open','tskill.exe',' Windows 10 Logon Background Changer ','',SW_HIDE,ewNoWait,ErrorCode);
    Result := True;
  end;
end;

我怎样才能纠正那个超级错误的代码?

提前致谢。

【问题讨论】:

    标签: inno-setup uninstallation


    【解决方案1】:
    function IsProcessRunning(const AppName: string): Boolean;
    var
      WMIService: Variant;
      WbemLocator: Variant;
      WbemObjectSet: Variant;
      Query: string;
    begin
      WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
      Query := 'SELECT * FROM Win32_Process Where Name="' + AppName + '"';
      WbemObjectSet := WMIService.ExecQuery(Query);
      Result := not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0);
    end;
    
    const
      ExeName = 'W10logbcr.exe';
    
    function InitializeUninstall(): Boolean;
    var
      ErrorCode: Integer;
    begin
      if IsProcessRunning(ExeName) then
      begin
        if SuppressibleMsgBox(
             'Windows 10 Logon Background Changer is running. ' +
             'Do you like to close it automatically and uninstall?',
             mbError, MB_YESNO, IDYES) <> IDYES then
        begin
          Result := False;
          exit;
        end
          else
        begin
          ShellExec(
            'open', 'taskkill.exe', '/f /im ' + ExeName, '', SW_HIDE,
            ewNoWait, ErrorCode);
        end;
      end;
    
      Result := True;
    end;
    

    请注意,我已经删除了 tskill.exe,因为我认为这与现在无关。只有家庭版的 Windows XP 甚至更早版本的 Windows 都需要它。而且它实际上不再适用于 Windows 10。

    另请注意,我已将您的 IsProcessRunning 替换为更好的 implementation by @ariwez

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2016-01-14
      • 2016-05-28
      • 1970-01-01
      • 2011-09-18
      • 2019-03-23
      • 2015-10-19
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多