Tip:

How to install a Windows service using script


 
It is possible to install and uninstall a Windows service using Visual Installer's script language. The script commands to use are XRUN and UNINSTALL_XRUN, and by calling them at Visual Installer's installation and uninstallation process, you have the tools you need to install and uninstall a Windows service.
 
The actual installation and uninstallation of the service are done by external programs. But with XRUN and UNINSTALL_XRUN you have the possibility to run these programs at the right moment, and the possibility to pass correct command line parameters to them.
 
Which program to call depends of which framework and programming language that was used to create the service. For example, if the service was created using Microsoft .NET Framework, a program with the filename InstallUtil.exe must be run. If the service was created using unmanaged C/C++, you must call the program itself with correct parameters. Generally, these three methods are used to install and uninstall a Windows service:
  
Method 1: The service is a .NET program
The service is written in C# or VB, and uses .NET Framework.
 
To install this kind of service you must run a program with the filename InstallUtil.exe, that was installed with .NET Framework. The path to the program depends of which version of .NET your program uses (there exist different versions of InstallUtil.exe). If for example your service is written using .NET 2.0, the path to the program is:
 
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe
  
This program takes command line parameters. One important parameter is the file path to your service. This must always be specified and must be a full path to your service program file. If you want to uninstall the service, you need also include /u as a parameter. If you omit /u, the service is installed. The line below shows how to install a service:
  
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe "C:\MyService\MyService.exe"
  
And the line below shows how to uninstall the same service:
  
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u "C:\MyService\MyService.exe"
  
More information about the InstallUtil.exe program is available on this page at Microsoft's website.
 
Method 2: The service is an unmanaged C/C++ program
The service is written in unmanaged C/C++.
 
To install this kind of a service you must run the program itself with suitable parameters. To install the service you must run the program using the parameter -install. For example like:
 
MyService.exe -install
 
To uninstall the service you must run the program using the parameter -uninstall. For example like:
 
MyService.exe -uninstall
 
More information is available on this page at Microsoft's website.
  
Method 3: A general method
There is also a general method available.
 
There is third method that can be used to install and uninstall a service. There is a program available in Windows with the filename SC.exe that handles services, and this program can be used to install and uninstall a service. The program can also be used to start and stop a service, and much more. The program is normally located in Windows system folder, so you don't need to specify a path to the program to run it; you only need to specify the filename. The line below shows how to install a service using SC.exe:
  
SC.exe create MyService binpath="C:\Program Files\MyService\CppMyService.exe"
  
And the line below shows how to uninstall the same service:
  
SC.exe delete MyWindowsService
  
The create parameter is used to install the service and the delete parameter is used to uninstall the service. More information is available on this page at Microsoft's website.
   
 
Using Visual Installer's script language to install and uninstall service
 
Above we explained three methods that are available to install and uninstall a service. The
XRUN and UNINSTALL_XRUN script commands can be used to utilize the methods above to install and uninstall services. Below we will give script code examples for all three methods:
 
Method 1 - The service is a .NET program
// Install service
XRUN C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe, 1,
 /LogFile= "%DESTDIR\MyService.exe"
 
// Uninstall service during Visual Installer's uninstallation process
UNINSTALL_XRUN C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe, 1,
 /u "%DESTDIR\MyService.exe"
 
Method 2 - The service is an unmanaged C/C++ program
// Install service
XRUN %DESTDIR\MyService.exe, 1, -install
 
// Uninstall service during Visual Installer's uninstallation process
UNINSTALL_XRUN %DESTDIR\MyService.exe, 1, -uninstall
 
Method 3 - A general method
// Install service
XRUN SC.exe, 1, create MyService binpath="%DESTDIR\MyService.exe"
 
// Uninstall service during Visual Installer's uninstallation process
UNINSTALL_XRUN SC.exe, 1, delete MyService
 
The script lines above can be entered in the After installation tab in the Execute script commands windows. Below is an example:


 
The
XRUN command is executed during the installation process and the UNINSTALL_XRUN command is executed during the uninstallation process.
 
The actual service program file (MyService.exe in our examples above) can be installed (copied) to the user's hard disk just by adding it to Visual Installer's file list. Also the uninstallation (deletion) of the actual file will be handled automatically in this way. So no script is needed to copy or delete the file. The script commands described above is only used to register and unregister the service.
 
 
Note. You may have noticed that we have included the parameter /LogFile= (without a file path) when we run InstallUtil.exe to install a service. This is to prevent log files from being created during the installation of the service. If you want log files to be created, you can omit this parameter. This page at Microsoft's website explains more.
   
 
More information about the XRUN and UNINSTALL_XRUN commands
 
The
XRUN and UNINSTALL_XRUN script commands, that we use in examples above, are used to run external programs during an installation and uninstallation process. The XRUN command is used to run a program during the installation and the UNINSTALL_XRUN command is used to run a program during the uninstallation (if the user makes such one). These two commands make it easy to install and uninstall programs that needs special processing during the installation and uninstallation process. More details about these two commands are available on this page.
    

< Tips Index Page
   

Go to Visual Installer product page Visual Installer Tip
 
Only Professional Version
SamLogic