Wix: how to forcefully kill a process/task?
Asked Answered
M

1

6

I need to forcefully kill a process that is running in the background before attempting to delete any files, when running an Uninstall from an MSI created with Wix. The main application consist of a trayicon which reflects the status of the bg-process monitoring local windows services (made on C#, though this may not be so relevant going further).

I first tried the following:

<File Id='FooEXE' Name='Foo.exe' Source='..\Source\bin\Release\Foo.exe' Vital='yes' />     
...
<InstallExecuteSequence>
  <Custom Action="CloseTray" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="CloseTray" ExeCommand="-exit" FileKey="FooEXE" Execute="immediate" Return="asyncWait" />

The tray icon is immediately closed after confirming application-close dialog, but the Foo.Exe task still appears on the taskmgr after the uninstall completed. Also,the following error message was given:

Error Message #1

Thats why, then I tried this:

<InstallExecuteSequence>
  <Custom Action="Foo.TaskKill" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="Foo.TaskKill" Impersonate="yes" Return="asyncWait" Directory="WinDir" ExeCommand="\System32\taskkill.exe /F /IM Foo.exe /T" />

After obtaining the same result, tried:

<Property Id="QtExecCmdLine" Value='"[WinDir]\System32\taskkill.exe" /F /IM Foo.exe'/>
...
<InstallExecuteSequence>
  <Custom Action="MyProcess.TaskKill" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="MyProcess.TaskKill" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="immediate" Return="ignore"/>

Sample which I took from here: How to kill a process from WiX

lately when all else failed, I also tried this without any success:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
...
<InstallExecuteSequence>
 <Custom Action="WixCloseApplications" Before="InstallValidate" />
</InstallExecuteSequence>
...
<util:CloseApplication Id="CloseFoo" CloseMessage="yes" Description="Foo is still running!" ElevatedCloseMessage="yes" RebootPrompt="yes" Target="Foo.exe" />

This one gave me a different error:

Error #2, What am I doing wrong here?

I'm thinking on building a statue in honor to this process that just refuses to die!!! ... either that or think a problem on the application side exists, where I should add something like Application.Exit(); or Environment.Exit(0); at some line inside Program.cs.

Is there any other thing I could do at either Wix or my application to attempt closing it successfully at Uninstall? Thanks!

Mere answered 14/5, 2013 at 3:33 Comment(0)
S
3

Personally I think the best option for you to go with is the in-built CloseApplication method rather than your previous options.

The error you are getting for that (Error code 2762) is because you are trying to schedule the action in the immediate sequence but have the ElevatedCloseMessage="yes" set which triggers it as a deferred action. Either remove this attribute or schedule it in the deferred sequence.

Siamese answered 14/5, 2013 at 11:19 Comment(2)
Well, that solved the second problem :) Thanks! Now I have to find out WHY neither of these kill the task and leave it alive (I still see Foo.exe on the taskmgr and the first error message too).Mere
Which user is running the app, the same that you are installing with? If not it will need to be run as a deferred action so that the system account kills the process.Siamese

© 2022 - 2024 — McMap. All rights reserved.