How to Reset Windows Service with Windows Installer Xml(WiX)

We will use the sample product.wxs file again to show you how to start/stop Windows Services.

  1. Create an xml file: product.wxs(copied from wix.chm)  as below
    This wix file will create an MSI file to copy readme.txt into %sysdrv%/program files/test program/
    <?xml version='1.0'?>
    <WiX xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>
       <Product Id='12345678-1234-1234-1234-123456789012' Name='Test Package' Language='1033'
                Version='1.0.0.0' Manufacturer='Microsoft Corporation'>
          <Package Id='12345678-1234-1234-1234-123456789012'
                    Description='My first Windows Installer package'
                    Comments='This is my first attempt at creating a Windows Installer database'
                    Manufacturer='Microsoft Corporation' InstallerVersion='200' Compressed='yes' />
     
          <Media Id='1' Cabinet='product.cab' EmbedCab='yes' />
     
          <Directory Id='TARGETDIR' Name='SourceDir'>
             <Directory Id='ProgramFilesFolder' Name='PFiles'>
                <Directory Id='MyDir' Name='TestProg' LongName='Test Program'>
                   <Component Id='MyComponent' Guid='12345678-1234-1234-1234-123456789012'>
                      <File Id='readme' Name='readme.txt' DiskId='1' src='readme.txt' />
                   </Component>
                </Directory>
             </Directory>
          </Directory>
     
          <Feature Id='MyFeature' Title='My 1st Feature' Level='1'>
             <ComponentRef Id='MyComponent' />
          </Feature>
       </Product>
    </WiX>
  2. Compile WiX Sample:
    c:\wix\candle product.wxs
  3. Link WiX object into MSI:
    c:\wix\light product.wixobj 
  4. Test your newly created MSI:
    c:\wix\msiexec /i product.msi
  5. Now if you want to start or stop services from your MSI, you can use WiX's ServiceControl node.

    ServiceControl

    Description
    Starts, stops, and removes services for parent Component. This element is used to control the state of a service installed by the MSI or MSM file by using the start, stop and remove attributes. For example, Start='install' Stop='both' Remove='uninstall' would mean: start the service on install, remove the service when the product is uninstalled, and stop the service both on install and uninstall.
    Parents
    Component
    Inner Text
    None
    Children
    Sequence (min: 1, max: 1)
    1. ServiceArgument (min: 0, max: unbounded): ordered list of arguments when modifying services
    Attributes
    Name Type Description Usage
    Id xs:string   required
    Name xs:string   required
    Remove xs:enumeration Possible values: {'install', 'uninstall', 'both'}.
     
    Start xs:enumeration Possible values: {'install', 'uninstall', 'both'}.
     
    Stop xs:enumeration Possible values: {'install', 'uninstall', 'both'}.
     
    Wait YesNoType  
                   
  6. Now you can modify product.wxs to control some services. In the sample below, I will show you how to use SericeControl to do an IISRESET with WiX.                  

                    
                    
    
    <?xml version='1.0'?>
    <WiX xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>
       <Product Id='12345678-1234-1234-1234-123456789012' Name='Test Package' Language='1033'
                Version='1.0.0.0' Manufacturer='Microsoft Corporation'>
          <Package Id='12345678-1234-1234-1234-123456789012'
                    Description='My first Windows Installer package'
                    Comments='This is my first attempt at creating a Windows Installer database'
                    Manufacturer='Microsoft Corporation' InstallerVersion='200' Compressed='yes' />
     
          <Media Id='1' Cabinet='product.cab' EmbedCab='yes' />
     
          <Directory Id='TARGETDIR' Name='SourceDir'>
             <Directory Id='ProgramFilesFolder' Name='PFiles'>
                <Directory Id='MyDir' Name='TestProg' LongName='Test Program'>
                   <Component Id='MyComponent' Guid='12345678-1234-1234-1234-123456789012'>
                      <File Id='readme' Name='readme.txt' DiskId='1' src='readme.txt' />
                      <ServiceControl Id='ResetWeb' Name='W3SVC' Start='both' Stop='uninstall' Wait='yes'/>
                   </Component>
                </Directory>
             </Directory>
          </Directory>
     
          <Feature Id='MyFeature' Title='My 1st Feature' Level='1'>
             <ComponentRef Id='MyComponent' />
          </Feature>
       </Product>
    </WiX>
Note:
The red part is the addition to reset an NT service. W3SVC is the service name. You can use Start and Stop attributes to get different behaviors.

See also

  • Introduction to the Windows Installer XML(WiX) Toolset
  • MSI basics
  • WiX Tutorial - Steps to Create an MSI with Windows Installer XML(WiX)
  • How to Install a COM DLL with Windows Installer XML(WiX)
  • How to Add a License Agreement UI to Windows Installer XML(WiX)
  • How to Add Customization Code CustomAction to Windows Installer XML(WiX)
  • How to Reset Windows Service with Windows Installer XML(WiX)
  • How to Troubleshoot MSI Installation
  • How to Pass Parameters to the Customization Code(CustomAction)
  • How to Log Tracing Infomation to the Calling MSI from CustomAction
  • How to Invoke InstallUtil.exe to Call Your Managed Installer Class
  • How to Create a Website and Virtual Directory with Windows Installer XML(WiX)
  • How to Install Files to an Arbitrary Location Outside "Program Files"
  • How to Read Configuration Parameters from Xml File
  • How to Use Environmental Variables and WiX Preprocessor
  • Download WiXSharp



  • THIS POST IS PROVIDED "AS-IS" WITH NO WARRANTIES AND CONFERS NO RIGHTS. Build time: Sun 01/27/2008 . ©2007 Dalun Software. All rights reserved. Back to Article List