Monday, August 13, 2007

WiX: Installing a Windows NT User-Defined Service for Windows NT Applications

A person by the name of Dan asked if I knew how to use WiX to install a Windows NT User-Defined Service using the INSTSRV.EXE and SRVANY.EXE files supplied with the Windows NT Resource Kit. 

I didn't, but after reading Microsoft Knowledge based article 137890 I put together this little sample that seems to work just fine.  Hope it works for you too Dan.  Enjoy :-)

You can download this sample from here

<?xml version="1.0" encoding="UTF-8"?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"

     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

  <!--This WiX package was created based on the MS knowledge base article "How To Create a User-Defined Service"

    http://support.microsoft.com/kb/137890

  -->

 

  <Product Id="a619b377-59a6-4a91-b32f-82eb3c71374e"

           Name="MyNotePadService" Language="1033" Version="1.0.0.0"

           Manufacturer="MyCompanyName"

           UpgradeCode="6d7aa4d3-1148-4af0-b2a9-7f36d47f4998">

    <Package InstallerVersion="200" Compressed="yes" />

 

    <Media Id="1" Cabinet="SRVANY.cab" EmbedCab="yes" />

 

    <!-- This should reflect the path where Instsrv.exe and Srvany.exe are installed-->

    <Property Id="RESKIT"

              Value="C:\Program Files\Windows Resource Kits\Tools\" />

    <!-- This should reflect the path of the application you want to run as a service -->

    <Property Id="TARGETDIR"

              Value="C:\Windows\System32\" />

    <!-- This should reflect the name you want to give the Service-->

    <Property Id="SERVICENAME" Value="Notepad" />

 

    <!-- Replace Notepad.exe with the name of the application you want to run as a service-->

    <Directory Id="TARGETDIR" Name="SourceDir">

      <Component Id="C_Registry"

                 Guid="{2B4934D1-9AAE-4f68-888C-CEC4EA7B42EA}">

        <RegistryKey Root="HKLM"

                  Key="SYSTEM\CurrentControlSet\Services\[SERVICENAME]\Parameters"

                  Action="create" />

        <RegistryValue Root="HKLM"

                  Key="SYSTEM\CurrentControlSet\Services\[SERVICENAME]\Parameters"

                  Action="write" Name="Application"

                  Type="string"

                  Value="[TARGETDIR]Notepad.exe"/>

      </Component>

    </Directory>

 

    <!-- Command line to install the service-->

    <CustomAction Id="CmdLine1"

                  Property="CmdLine1_PROP"

                  Value="[SystemFolder]cmd.exe" />

    <CustomAction Id="CmdLine2"

                  Property="CmdLine1_PROP"

                  ExeCommand='/c ""[RESKIT]Instsrv.exe" [SERVICENAME] "[RESKIT]Srvany.exe""'

                  Return='ignore'/>

    <!-- Command line to Uninstall the service-->

    <CustomAction Id="CmdLine3"

                  Property="CmdLine3_PROP"

                  Value="[SystemFolder]cmd.exe"

                  Return='check'/>

    <CustomAction Id="CmdLine4"

                  Property="CmdLine3_PROP"

                  ExeCommand='/k ""[RESKIT]Instsrv.exe" [SERVICENAME] REMOVE"'

                  Return='ignore'/>

 

    <InstallExecuteSequence>

      <!-- Installs the service on install-->

      <Custom Action="CmdLine1"

              After="InstallFinalize">NOT Installed</Custom>

      <Custom Action="CmdLine2"

              After="CmdLine1">NOT Installed</Custom>

      <!-- Removes the service on Uninstall-->

      <Custom Action="CmdLine3"

              Before="RemoveFiles">Installed</Custom>

      <Custom Action="CmdLine4"

              After="CmdLine3">Installed</Custom>

    </InstallExecuteSequence>

 

    <Feature Id="ProductFeature" Title="[SERVICENAME]" Level="1">

      <ComponentRef Id="C_Registry" />

    </Feature>

  </Product>

</Wix>

2 comments:

Anonymous said...

Thanks so much, Jeff! I'll put this to good use.

Jeff Wharton said...

No problem. Let me know how you go.