MAKEMSI quickly and reliably creates MSI files in a non-programmatic way
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
[Bottom][Contents][Prev]: SelfRegister[Next]: ServiceInstall
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
\->Source Code->Commands->ServiceControl

The "ServiceControl" Command

This command is used to stop, start or delete services during install and uninstall. It populates the MSI "ServiceControl" table.

The service being controlled is commonly installed by the same package using the "ServiceInstall" command but it could just as easily be a pre-existing service.

You should also have a look at the "service tips" section of this manual.

Main Service Related Options

Please see the "options for commands" section of the manual.

#define? DEFAULT_SERVICE_CONTROL_INSTALL_EVENTS    STOP START
#define? DEFAULT_SERVICE_CONTROL_UNINSTALL_EVENTS  STOP DELETE
#define? DEFAULT_SERVICE_INSTALL_PROCESS           OWN
#define? DEFAULT_SERVICE_INSTALL_START             AUTO
#define? DEFAULT_SERVICE_INSTALL_ERRORS            NORMAL VITAL

The Parameters

This command takes these parameters:

The WAIT Parameter

You should wait unless you know of a reason why you can't, you could if required create multiple control entries so that you only don't wait when you absolutely can't. Whatever your options it is a good idea to test this well to confirm it reacts as you'd expect.

If you have a service which relies on hardware and you want this to install whether or not the hardware is attached at MSI install time then you should not wait for the service to start.

If you are simply controlling a service (stopping and starting) where it always exists (perhaps your MSI validates as separate step) then you should wait otherwise you may be performing an action thinking that the service is stopped but it isn't (its "stopping"). I have also seen Windows Installer fail to start a service, probably because it was still stopping while it tried to start it again.

If you are not waiting and during uninstall have "stop" and "delete" then you may or may not see the service listed after uninstall in a "disabled" state, this will disappear after a reboot, however a subsequent reinstall sometimes fails (prior to this reboot).

EXAMPLE - Isolated

<$ServiceControl Name="SomeService" AtInstall="start" AtUninstall="">

In the above "AtUninstall" was given a blank value, if you had not specified it then the default would have been used which may be undesirable.

EXAMPLE - Service Installation

;----------------------------------------------------------------------------
;--- Some service constants -------------------------------------------------
;----------------------------------------------------------------------------
#define MyServiceAlias         MyService
#define MyServiceDesc          My Test Service (<$MSI_MSIBASENAME>)
#define RecoveryResetInSeconds <?=6*60*60>  ;;6 hours
#( '/'
   #define RecoveryActions
   restart/5000                ;;1.  Restart after 5 seconds
   restart/60000               ;;2.  Restart after 1 minute
   restart/600000              ;;3+. Restart after 10 minutes
#)


;----------------------------------------------------------------------------
;--- Add service ------------------------------------------------------------
;----------------------------------------------------------------------------
<$DirectoryTree Key="INSTALLDIR" Dir="[ProgramFilesFolder]\MyServiceDirectory" CHANGE="\">
<$Component "<$MyServiceAlias>" Create="Y" Directory_="INSTALLDIR">
   ;--- The service EXE MUST be the keypath of the component ----------------
   <$Files ".\MyService.EXE" KeyFile="*">

   ;--- Create the service (service EXE is "KeyPath" of component) ----------
   #(
       <$ServiceInstall
                  Name="<$MyServiceAlias>"
           DisplayName="<$MyServiceDesc>"
           Description="<$MyServiceDesc>"
               Process="Own Interactive"
       >
   #)

   ;--- Control the service -------------------------------------------------
   <$ServiceControl Name="<$MyServiceAlias>" AtInstall="start stop" AtUninstall="stop delete">
<$/Component>


;----------------------------------------------------------------------------
;--- Need sc.exe available at install time (the CA below uses it) -----------
;----------------------------------------------------------------------------
<$Component "ServiceTool" Create="Y" Directory_="INSTALLDIR">
   <$Files ".\sc.exe" Comment=^This tool is used to set the services recovery options.^>
<$/Component>


;----------------------------------------------------------------------------
;--- Create script to execute "sc.exe" to set recovery options --------------
;----------------------------------------------------------------------------
#data "CaData"
   "INSTALLDIR" '[INSTALLDIR]'
#data
<$VbsCa Binary="SetServiceRecovery.vbs" DATA=^CaData^>
   ;--- INSTALL -------------------------------------------------------------
   <$VbsCaEntry "JustBeforeStartServices">
        ;--- Call generic set recovery routine ------------------------------
        SetRecovery "<$MyServiceAlias>", <$RecoveryResetInSeconds>, "<$RecoveryActions>"
   <$/VbsCaEntry>

   <?NewLine><?NewLine>
   '=========================================
   sub SetRecovery(ByVal ServiceAlias, ByVal RecoveryResetInSeconds, ByVal RecoveryActions)
   '=========================================
       ;--- Initialization --------------------------------------------------
       CaDebug 1, "Set service recovery for """ & ServiceAlias & """"
       VbsCaLogInc 1
       dim ScExe   : ScExe   = VbsCaCadGet("INSTALLDIR") & "sc.exe"
       dim ScArgs  : ScArgs  = "failure """ & ServiceAlias & """ reset= " & RecoveryResetInSeconds  & " actions= " & RecoveryActions
       dim ScArgsQ : ScArgsQ = "qfailure """ & ServiceAlias & """"

       ;--- Set the recovery information ------------------------------------
       CaDebug 2, "Setting up the services recovery options"
       VbsCaRunSync """" & ScExe & """ " & ScArgs, 0, ""  ;;Ignore RC (can't be trusted)

       ;--- Log the current state of the recovery options -------------------
       CaDebug 2, "Logging the services recovery options"
       VbsCaRunSync """" & ScExe & """ " & ScArgsQ, 0, ""

       ;--- Finished this service -------------------------------------------
       VbsCaLogInc -1
   end sub
<$/VbsCa>


;----------------------------------------------------------------------------
;--- Call the above script during install and uninstall (schedule them) -----
;----------------------------------------------------------------------------
<$VbsCaSetup Binary="SetServiceRecovery.vbs" Entry="JustBeforeStartServices" Seq="InstallServices-" CONDITION=^<$CONDITION_INSTALL_ONLY>^ DATA=^CaData^>


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: SelfRegister[Next]: ServiceInstall


MAKEMSI© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday February 28 2016 at 3:45pm
Visit MAKEMSI's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.