Implement Scripts in a ThinApp Environment
You might implement a script in the following circumstances:
Running a .bat file from a network share inside the virtual environment.
Importing the .reg file at runtime.
Implement a script
Save the script contents in a plain text file with the .vbs extension in the same directory as your Package.ini file.
You can use any filename. ThinApp adds all .vbs files to the package at build time.
.bat Example
The following script runs an external .bat file from a network share inside the virtual environment. The .bat file makes modifications to the virtual environment by copying files, deleting files, or applying registry changes using regedit /s regfile.reg. Run this script only for the first parent process. If you run this script for other processes, each copy of the cmd.exe utility runs the script and an infinite recursion develops.
Function OnFirstParentStart
Set Shell = CreateObject("Wscript.Shell")
Shell.Run "\\jcdesk2\test\test.bat"
End Function
Timeout Example
The following script prevents the use of an application after a specified date. The VBS date uses the #mm/dd/yyyy# format, regardless of locale.
This check occurs upon startup of the parent process and any child processes.
if Date >= #03/20/2007# then
msgbox "This application has expired, please contact Administrator"
ExitProcess 0
end if
Modify the Virtual Registry
The following script procedure modifies the virtual registry at runtime to load an external ODBC driver from the same directory where the package executable file is located.
Modify the registry
Origin = GetEnvironmentVariable("TS_ORIGIN")
LastSlash = InStrRev(Origin, "\")
SourcePath = Left(Origin, LastSlash)
Form a new path to the ODBC DLL file located outside of the package.
DriverPath=SourcePath + "tsodbc32.dll"
Set WSHShell = CreateObject("Wscript.Shell")
This modification causes the application to load the DLL from an external location.
.reg Example
The following script imports the registry values from an external .reg file into the virtual registry at runtime.
Function OnFirstParentStart
ExecuteVirtualProcess "regedit /s C:\tmp\somereg.reg"
End Function
Stopping a Service Example
The following script stops a virtual or native service when the main application quits.
Function OnFirstParentExit
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "net stop ""iPod Service"""
End Function
Copying a File Example
The following script sections shows how to copy a configuration file located in the same directory as the captured executable file into the virtual file system each time the application starts. This script is useful for an external configuration file that is easy to edit after deployment. Because the copy operation occurs each time you run the application, any changes to the external version are reflected in the virtual version.
For example, if your captured executable file is running from \\server\share\myapp.exe, this script searches for a configuration file located at \\server\share\config.ini and copies it to the virtual file system location at C:\Program Files\my application\config.ini.
By putting this code in the OnFirstParentStart function, it is only called once each time the script runs. Otherwise it runs for every child process.
Function OnFirstParentStart
ThinApp sets up TS_ORIGIN to indicate the full path to a captured executable file package. A virtual application sets the TS_ORIGIN variable to the physical path of the primary data container.If you have a virtual application consisting of the main.exe and shortcut.exe files, both files reside in C:\VirtApp. When you run the main.exe file, TS_ORIGIN var is set to C:\VirtApp\main.exe. When you run the shortcut.exe file, the TS_ORIGIN environment variable is set to C:\VirtApp\main.exe. The environment variable is always set to the primary data container, even when you create a shortcut. When you run VBScripts that are included in the package, the variable is already set and available to the scripts.
Origin = GetEnvironmentVariable("TS_ORIGIN")
You can separate the filename from TS_ORIGIN by finding the last backslash and removing all of the characters following it.
LastSlash = InStrRev(Origin, "\")
SourcePath = Left(Origin, LastSlash)
The source file to copy into the virtual environment is the package path plus config.ini.
SourceFile = SourcePath + "Config.ini"
The location to copy to might be a different location on different computers if the Program Files directory is mapped to a location other than C:\. The following call lets ThinApp expand a macro to obtain the correct location for the local computer.
DestFile = ExpandPath("%ProgramFilesDir%\MyApplication\Config.ini")
Use the file systemObject parameter to check the source file exists.
Set objFSO = CreateObject("Scripting.filesystemObject")
If objFSO.FileExists(SourceFile) Then
If the source file exists, copy it into the virtual file system. The %ProgramFilesDir%\MyApplication virtual directory is in the package.
objFSO.CopyFile SourceFile, DestFile, TRUE
End if
End Function
Add a Value to the System Registry
This script procedure adds a value to the physical system registry.
Add a value to the system registry
Create a .reg file and run the regedit /s command as an external process that accesses the system registry instead of the virtual registry.
Function OnFirstParentStart
Create the .reg file in a location that has the IsolationMode parameter set to Merged so that the virtual environment can access it with this script and the physical environment can access it with the regedit /s command.
RegFileName = ExpandPath("%Personal%\thin.reg")
Set fso = CreateObject("Scripting.filesystemObject")
Set RegFile = fso.CreateTextFile(RegFileName, true)
The %Personal% directory is a directory that has Merged isolation mode by default.
Construct the .reg file.
RegFile.WriteLine("Windows Registry Editor Version 5.00")
RegFile.WriteLine(chr(34) & "InventoryName" & chr(34) & "=" & chr(34) & GetBuildOption("InventoryName") & chr(34))
RegEditPid = ExecuteExternalProcess("regedit /s " & chr(34) & RegFileName & chr(34))
WaitForProcess RegEditPid, 0
Wait until the process is complete.
End Function