ThinApp Scripting: Launch virtual application only if a specific process is not running

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.

<

p class=”lang:vb decode:true crayon-selected”>

' Run script if an entry point is launched<br>Function OnFirstParentStart</p>
' 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.

6 thoughts on “ThinApp Scripting: Launch virtual application only if a specific process is not running

  1. 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

    Like

    1. 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!😉

      Like

  2. 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 & “rootcimv2”)
    Source: Microsoft VBScript Runtime Error
    Description: Object Required: ‘objWMIService’

    It seems that GetObject is not returning correctly, but only when run inside…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s