In my daily life as Consultant for VMware I implement a lot of customer specific scripts using the ThinApp scripting engine and I just thought: Why not share these scripts?
The following script can be used if a ThinApp application can or should not be launched if a specific process is already running. In this particular case a virtualized instance of SAP should only be able to launch if no native instance of SAP is running already.
' Run script if an entry point is launched
Function OnFirstParentStart
' Initialize variables
Dim objWMIService, objWMIProcess, objShell
Dim colWMIProcess
Dim strTitle, strMessage, strComputer, strProcess
Dim intButton
' Set computer to localhost
strComputer = "."
' Set process to check
strProcess = "saplogon.exe"
' Set error message
strTitle = "VMware ThinApp"
strMessage = "The process " & strProcess & "is already running!" & Vbcrlf & "Please close the process and then try again."
intButton = 48
' Connect to WMI service
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colWMIProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name='" & strProcess & "'")
If colWMIProcess.Count > 0 Then
' Display message box
Msgbox strMessage, intButton, strTitle
ExitProcess 0
End If
' End of OnFirstParentStart Function
End Function
To implement this script in your ThinApp project just save the code above as .vbs file in your ThinApp projects folder und customize the variable strProcess. Just enter the process you want to check and you are ready.
You can also customize the error message the user gets by modifying the variable strMessage.

Pingback: ThinApp Scripting: Launch virtual application only if a specific process is not running at That's my View
When possible, you should do your login right in your WMI query. In this case the following changes will make the script return much faster on a system with lots of processes running:
[22] Set colWMIProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name='" & strProcess & "'")
[23] If colWMIProcess.Count > 0 Then
[24] ' Display message box
[25] Msgbox strMessage, intButton, strTitle
[26] ExitProcess 0
[27] End If
[28] ' End of OnFirstParentStart Function
[29]End Function
LOGIC, not LOGIN.. /sigh
Sorry that should have read “..you should do your LOGIC right in your…”
Hi Jorgie, thanks for your tip. You are right, your code is much more efficient and therefore I updated the post with it. Thanks, much appreciated. I actually used the original script logic to check for more than one process and modified it to work with just one process. It seems I forgot to optimize the script afterwards!
This doesn’t work for me…
The script works fine when run individually (assuming you call the function first), but when it is compiled and run from the ThinApp’d application, an error message appears. I’m wondering if the WMI object will only work if the application is ThinApp’d with specific settings (perhaps changing isolation mode)?
Error Line 21, Char 1: Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Source: Microsoft VBScript Runtime Error
Description: Object Required: ‘objWMIService’
It seems that GetObject is not returning correctly, but only when run inside…
I just tested the script again with ThinApp 4.7.3 on a Windows 7 host and it worked without any problem. I haven’t set any specific isolation mode.