Hey I need to uninstall my program when it is running in Windows using Inno Setup.
I like my uninstaller to detect, if it is running and give a message box to user like "Windows 10 Logon Background Changer is running. Do you want to close it and uninstall?"
The message box described above should have two buttons (Yes and No).
When this message box appears after user choose to uninstall this even it is running, and when user choose No in the message box, the uninstaller should close.
If user choose Yes in that message box, the uninstaller should kill the running program process and uninstall it normally (not silently).
I written a code to do so, but it is failing and it gives wrong behaviors.
My code is:
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;
How can I correct that super-wrong code?
Thanks in advance.