vkreddy
vkreddy

Reputation: 191

WIX - How to pass file location which is read from registry to Custom Action?

I have a batch script in Installation Directory. I have to execute this batch script as part of MSI Uninstall.

Firstly, To know the location of the Install directory, I am doing RegistrySearch with below piece of code

 <Property Id="REGISTRY_RESULT">
          <RegistrySearch Id="MyRegistrySearch" 
              Root="HKCU" 
              Key="Software\InstallPath" 
              Name="path"
              Type="raw" />
 </Property>

I have created custom action to call the batch script.

    <SetProperty Id="CMD" Value="C:\Windows\System32\cmd.exe" Before="CostFinalize" />
    <CustomAction Id="RunBat" Property="CMD" Execute="deferred" Impersonate="no" Return="check"  ExeCommand="[**??REGISTRYSEARCH??**}\RunBat.bat"/>

Custom Action already has one Property Set to "CMD". Now, How to pass REGISTRY_RESULT to my Custom action? Can someone help?

Upvotes: 1

Views: 1069

Answers (1)

Mykhailo Seniutovych
Mykhailo Seniutovych

Reputation: 3691

This is how I managed to do this (without using SetProperty):

<Property Id="REGISTRY_RESULT">
  <RegistrySearch Id="MyRegistrySearch"
      Root="HKLM"
      Key="Software\MyProgram"
      Name="InstallDir"
      Type="raw"/>
</Property>

<CustomAction Id="RunBat" Directory="INSTALLFOLDER" Execute="deferred" Impersonate="no" Return="check"  ExeCommand="[SystemFolder]cmd.exe /C [REGISTRY_RESULT]\RunBat.bat"/>

This is the full code of my test wix project that I created, in case you need it:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="ConsoleApp" Language="1033" Version="1.0.0.0" Manufacturer="SoftwareCompany" UpgradeCode="8b78c2e7-47cf-4b25-a0f9-4b648db7336e">
        <Package InstallerVersion="500" Compressed="yes" InstallScope="perMachine" />
        <MediaTemplate EmbedCab="yes"/>

        <Feature Id="ProductFeature" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="ConsoleApp" />
      </Directory>
    </Directory>

    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="cmpConsoleApp.exe" Guid="*">
        <File Id="ConsoleApp.exe" KeyPath="yes" Source="d:\Learning\WIX\WixTraining\ConsoleApp\bin\Debug\ConsoleApp.exe" />
      </Component>
    </ComponentGroup>

    <Property Id="REGISTRY_RESULT">
      <RegistrySearch Id="MyRegistrySearch"
          Root="HKLM"
          Key="Software\MyProgram"
          Name="InstallDir"
          Type="raw"/>
    </Property>

    <CustomAction Id="RunBat" Directory="INSTALLFOLDER" Execute="deferred" Impersonate="no" Return="check"  ExeCommand="[SystemFolder]cmd.exe /C [REGISTRY_RESULT]\RunBat.bat"/>

    <InstallExecuteSequence>
      <Custom Action="RunBat" Before="InstallFinalize">NOT Installed</Custom>
    </InstallExecuteSequence>

    </Product>
</Wix>

Notice how I wrote my CustomAction, I did not specify property attribute and instead used Directory (Wix forces you to have Directory attribute), I also used ExeCommand="[SystemFolder]cmd.exe /C [REGISTRY_RESULT]\RunBat.bat" whic basically means take cmd.exe from SystemFolder which is C:\Windows\System32\cmd.exe and run batch file RunBat.bat which is in REGISTRY_RESULT folder.

That worked for me, if you still have troubles running your batch file I assume there might be some problems with your RegistrySearch, I suggest you running your msi file with logging enabled (open cmd and type msiexec /i "PATH_TO_YOUR_MSI" /L*V "log.txt") then look into the log.txt and search for REGISTRY_RESULT property, if everything is okay you should see a line REGISTRY_RESULT = {PATH_TO_InstallDir}, if you don't see it than probably this key doesn't exist, alos make sure that you didn't mix up HKCU and HKLM and you indicated the correct one.

Upvotes: 1

Related Questions