2

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
flavalee
  • 57
  • 4

1 Answers1

3
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;

Note that I've removed the tskill.exe, as I believe that's irrelevant nowadays. It was needed on Home editions of Windows XP only and even older version of Windows. And it's actually not available on Windows 10 anymore.

Also note that I've replaced your IsProcessRunning with a better implementation by @ariwez.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992