Windows Installer, Java Installer, Freeware Installer
Home|Contact|Site Map|TOC|Search
Download  Features   Java  Licensing  Purchase  Testimonials  Support  Forums

How do I detect and stop a process?

Answer

When updating an installation it is recommended to stop the installed application before overwriting its files. Usually this is managed automatically by Windows Installer through the "FilesInUse" and "MsiRMFilesInUse" dialogs, but you can close your application whenever you want through a custom action.

Detect if a process is running

You can detect if a process is running by using a VBScript custom action which looks like this:

Dim objWMIService, objProcess, colProcess
Dim strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process")

Session.Property("PROCESS")="NOT RUNNING"
For Each objProcess in colProcess
if objProcess.Name = "AdvancedInstaller.exe" then
   Session.Property("PROCESS")="RUNNING"
End If
Next

This code can be pasted in a .VBS file which can be added as an Attached custom action under the "InstallExecuteSequence" -> "FindRelatedProducts" standard action. You can see this standard action by using the "Show Standard Action" button on the toolbar of the Custom Actions page.

ImportantMake sure that in the Custom Action Properties page the "Function Name" field is empty (the rest of the settings are the default ones). Also, schedule this custom action as Immediate in order to give it access to installer properties.

The above custom actions will search for the process named 'AdvancedInstaller.exe' (this process is running when Advanced Installer is opened). Based on the result of this search, the custom action sets the property PROCESS to RUNNING or NOT RUNNING. You can use this property as a custom launch condition for the installation package in order to stop the installation if the main application is running.

Stop a process

The custom action which detects if a process is running can be modified to stop a process if it detects it:

Dim objWMIService, objProcess, colProcess
Dim strComputer
Dim objname
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strProcessKill = "'AdvancedInstaller.exe'"

Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colProcess
    objname = objProcess.Name
If objProcess.Name = "AdvancedInstaller.exe" then
    objProcess.Terminate()
End If
Next 

You can add this code in a .VBS file and add the file as an Attached custom action. This custom action can be scheduled under a standard action which is executed before the process needs to be terminated.

ImportantMake sure that in the "Custom Action Properties" page the "Function Name" field is empty (the rest of the settings are the default ones). Also, schedule this custom action as Immediate in order to give it access to installer properties.

NoteThis sample custom actions detect the Advanced Installer process. In order to make them detect your process you need to replace the string "AdvancedInstaller.exe" with the name of the process you want to detect or stop.

On Windows Vista these custom actions can run only as Deferred with no impersonation. In this case, you can use the Action Data field in the Custom Action Properties page to pass the "PROCESS" (or another) property to the custom action. The value in the "Action Data" field can be retrieved inside the custom action from the CustomActionData property (for example Session.Property("CustomActionData")).

Privacy Policy | Windows Installer | Search Engine Ranking | Link Analyzer