【发布时间】:2016-11-02 15:16:42
【问题描述】:
嘿,当我的程序在 Windows 中运行时,我需要使用 Inno Setup 卸载它。
我喜欢我的卸载程序检测它是否正在运行并向用户提供消息框,例如 “Windows 10 Logon Background Changer 正在运行。您要关闭它并卸载吗?” p>
上面描述的消息框应该有两个按钮(Yes和No)。
当这个消息框出现在用户选择卸载它后,即使它正在运行,当用户在消息框中选择否时,卸载程序应该关闭。
如果用户在该消息框中选择是,卸载程序应该终止正在运行的程序进程并正常卸载它(不是静默)。
我为此编写了一个代码,但它失败了并且给出了错误的行为。
我的代码是:
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;
我怎样才能纠正那个超级错误的代码?
提前致谢。
【问题讨论】: