0

I'm using Wix via javafxpackager to build an MSI installer for my Java (8) app. When installing it, I can pass command line attributes, such as:

msiexec /i app.msi FOO=BAR

How do I go about accessing the value of FOO from my own application?

I already have a custom wxs file that javafxpackager is picking up (src/main/deploy/package/windows/<<APPNAME>>.wxs) and it looks like this

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="PRODUCT_GUID" Name="APPLICATION_NAME"
             Language="1033" Version="APPLICATION_VERSION"
             Manufacturer="APPLICATION_VENDOR"
             UpgradeCode="PUT-GUID-HERE">
        <Package Description="APPLICATION_DESCRIPTION" Comments="None"
                 InstallerVersion="200" Compressed="yes"
                 InstallScope="INSTALL_SCOPE" Platform="PLATFORM"/>
        <Media Id="1" Cabinet="simple.cab" EmbedCab="yes"/>

        <!-- We use RemoveFolderEx to ensure application folder is fully
             removed on uninstall. Including files created outside of MSI
             after application had been installed (e.g. on AU or user state).

             Hovewer, RemoveFolderEx is only available in WiX 3.6,
             we will comment it out if we running older WiX.

             RemoveFolderEx requires that we "remember" the path for uninstall.
             Read the path value and set the APPLICATIONFOLDER property with the value.
        -->
        <Property Id="APPLICATIONFOLDER">
            <RegistrySearch Key="SOFTWARE\APPLICATION_VENDOR\APPLICATION_NAME"
                            Root="REGISTRY_ROOT" Type="raw"
                            Id="APPLICATIONFOLDER_REGSEARCH" Name="Path"/>
        </Property>
        <DirectoryRef Id="APPLICATIONFOLDER">
            <Component Id="CleanupMainApplicationFolder" Guid="*" Win64="WIN64">
                <RegistryValue Root="REGISTRY_ROOT"
                               Key="SOFTWARE\APPLICATION_VENDOR\APPLICATION_NAME"
                               Name="Path" Type="string" Value="[APPLICATIONFOLDER]"
                               KeyPath="yes"/>
                <!-- We need to use APPLICATIONFOLDER variable here or RemoveFolderEx
                     will not remove on "install". But only if WiX 3.6 is used. -->
                WIX36_ONLY_START
                <util:RemoveFolderEx On="uninstall" Property="APPLICATIONFOLDER"/>
                WIX36_ONLY_END
            </Component>
        </DirectoryRef>
        <?include bundle.wxi ?>
        UI_BLOCK
        APP_CDS_BLOCK
        <Icon Id="DesktopIcon.exe" SourceFile="APPLICATION_ICON"/>
        <Icon Id="StartMenuIcon.exe" SourceFile="APPLICATION_ICON"/>
        SECONDARY_LAUNCHER_ICONS
        <MajorUpgrade Schedule="afterInstallInitialize"
                      DowngradeErrorMessage="A later version of this app is already installed. Setup will now exit."/>
        <Icon Id="icon.ico" SourceFile="app.ico"/>
        <Property Id="ARPPRODUCTICON" Value="icon.ico"/>
    </Product>
</Wix>
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • Took out the hard-coded upgrade code GUID to prevent it from being copy / pasted. If people copy your GUID, they could in theory accidentally uninstall your product during their own installation, or vice-versa (you uninstall them). Low risk, but it exists - some code is more likely to be copied than others. – Stein Åsmul Mar 25 '18 at 12:02

1 Answers1

1

You can write any public property (UPPERCASE) to the registry during your installation, and then you can read back that value from your application at runtime using regular registry access mechanisms (whatever constructs your development language supports).

Properties are not automatically persisted to the registry, you need to add them to the registry yourself. Perhaps check out The WiX toolset's "Remember Property" pattern.

You need to add:

<RegistryValue Root="HKLM" 
               Key="SOFTWARE\APPLICATION_VENDOR\APPLICATION_NAME"                                
               Name="Foo" Type="string" Value="[FOO]"/>

and

<Property Id="FOO">
    <RegistrySearch Id="Foo"
                    Root="HKLM"
                    Key="SOFTWARE\APPLICATION_VENDOR\APPLICATION_NAME"
                    Name="Foo" Type="raw"/>
</Property>

to the .wxs file. That will take care of recording the property in the registry and re-saving it when upgrading.

Various methods from accessing it from Java can be found on: read/write to Windows Registry using Java

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • javafxpackager generates the WiX config, but in my case, I'm already doing it manually because I need other customizations. – Pablo Fernandez Mar 25 '18 at 06:29
  • Thanks for pointing out about `/a`. I copied and pasted one of my experimental commands and forgot to change it. This is not what I'm after, so, I change my question to use `/i`. – Pablo Fernandez Mar 25 '18 at 07:12
  • OK, the `/a` administrative installation mix-up is very common, that's why I added the explanatory links. I see you inlined Rob's remember property WiX code. I don't have time to test it, but it looks OK, and coming from Rob it is always technically sound - do test though. All installation modes, including upgrades. I should have a look at `javafxpackager` - one more for the list of things to check! Sigh! :-). – Stein Åsmul Mar 25 '18 at 12:15