WiX: Passing Data to managed custom action does not work - wix

i got a problem regarding wix and managed custom actions: I already managed it to create an managed custom action and got it called from my installer (verified it in the installation log files). My Problem is now that i did't manage it to pass data to the custom action. Afaik if i choose to run the custom action in such a late stage i must mark the action as "deferred". With this limitation it's only possible to access the CustomActionData Property right? Why does this not work in my case? (As far as i can see i adoptet everything correctly from the samples?)
Here is what i already tried:
Custom Action:
public class CustomActions
{
[CustomAction]
public static ActionResult RegisterDflHelp(Session session)
{
session.Log("Begin CustomAction1");
session.Log("Before Access to customactiondata");
//string helpdir = session["HELP_DIR"];
string cad = session["CustomActionData"];
Debugger.Break();
session.Log("Help dir is: " + helpdir);
session.Log("Custom Action Data is: " + cad);
return ActionResult.Success;
}
}
Merge Module which calls custom action:
<CustomAction Id='RegisterDflHelp' BinaryKey='RegisterDflHelpDll' DllEntry='RegisterDflHelp' Execute='deferred' />
<CustomAction Id="HELP_DIR.SetProperty" Return="check" Property="HELP_DIR" Value="Teeest" />
<Property Id='HELP_DIR' Secure='yes'/>
<InstallExecuteSequence>
<Custom Action='HELP_DIR.SetProperty' After='InstallFiles' />
<Custom Action='RegisterDflHelp' After='HELP_DIR.SetProperty' />
</InstallExecuteSequence>
<Directory Id="TARGETDIR" Name="SourceDir">
</Directory>
<ComponentGroupRef Id="HelpGroup"/>
Product which includes Help-Merge Module:
<Product....>
<Package....>
...
<Directory>
<!--Directory which gets the help folder--!>
<Merge Id ="DflHelpInstaller" SourceFile="DflHelpInstaller.msm" Language="1033" DiskId="1" />
Anyone an idea? Thanks in advance!!
Daniel
Btw: What would also be interesting to know: At which stage in the installationprocess must an action marked as deferred? And if i managed it to call my custom action before this stage. Whats the advantage?

You are still setting the HELP_DIR property instead of the custom action data. If you are trying to set the CustomActionData for the RegisterDfHelp action to "Teeest", then I think you should do this:
<CustomAction Id="SetRegisterDflHelpCustomActionData" Return="check"
Property="RegisterDflHelp" Value="Teeest" />
<InstallExecuteSequence>
<Custom Action='SetRegisterDflHelpCustomActionData' After='InstallFiles' />
<Custom Action='RegisterDflHelp' After='SetRegisterDflHelpCustomActionData' />
</InstallExecuteSequence>

somethimes using your head really helps;-)
the reason why the sample above does not work is quite simple:
Instead of:
CustomAction Id="HELP_DIR.SetProperty" Return="check" Property="HELP_DIR" Value="Teeest" />
I head to set the Property Attribute to the Action which should get the custom data!! here:
<CustomAction Id="HELP_DIR.SetProperty" Return="check" Property="RegisterDflHelp" Value="$(var.HelpSourceDir)" />
Hope that helps others to save the time i spend to solve this silly problem;-)

Related

Custom Actions not been called

I have some problems with managed code custom actions. I have 3 custom actions but only one of them is working. They are called at different times in InstallExecuteSequence but moving them makes no difference. I know there not getting very far because if I place a message box at the beginning of the routine(for debugging) it never get called . Where am I going wrong ? The actions are created like so.
<Binary Id="CA" SourceFile="$(var.ca.Custom.Actions.TargetDir)$(var.ca.Custom.Actions.TargetName).CA.dll" />
<CustomAction Id="WriteRemoveArpEntry" Property="CustomActionData" Value="PNAME=$(var.ProductName)" HideTarget="yes" />
<CustomAction Id="RemoveArpEntry" BinaryKey="CA" DllEntry="RemoveProductFromARP" Return="ignore" />
<CustomAction Id="SetValueforProductFolder" Property="CustomActionData" Value="SDIR=[INSTALLDIR];TDIR=[MANUDIR]\backup\$(var.ProductName)\$(var.VersionNumber)" HideTarget="yes" />
<CustomAction Id="Backup_Product_DIR" BinaryKey="CA" DllEntry="BackupDIR" Return="ignore" />
<CustomAction Id="WriteInstallAttemptData" Property="CustomActionData" Value="PRODUCTNAME=$(var.ProductName);APPVERSION=$(var.VersionNumber)" HideTarget="yes" />
<CustomAction Id="WriteInstallAttempt" BinaryKey="CA" DllEntry="WriteXMLServer" Return="ignore" />
I then call them here
<Custom Action="SetValueforProductFolder" Before="Backup_Product_DIR">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="Backup_Product_DIR" Before="InstallFinalize">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="WriteRemoveArpEntry" Before="InstallFinalize">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="RemoveArpEntry" After="WriteRemoveArpEntry">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="WriteInstallAttemptData" After="InstallFinalize">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="WriteInstallAttempt" After="WriteInstallAttemptData">NOT Installed AND NOT REMOVE</Custom>
</InstallExecuteSequence>
The headers for the routines look like this
public static ActionResult BackupDIR(Session session)
{
public static ActionResult RemoveProductFromARP(Session session)
{
Public static ActionResult WriteXMLServer(Session session)
{
However only WriteXMLServer works. In the log file I get the following .
MSI (s) (BC:9C) [07:23:45:562]: Invoking remote custom action. DLL:
C:\Windows\Installer\MSI2E2A.tmp, Entrypoint: BackupDIR CustomAction
Backup_Product_DIR returned actual error code 1154 but will be
translated to success due to continue marking
In the one that works I get
MSI (s) (BC:A0) [07:24:25:994]: Invoking remote custom action. DLL:
C:\Windows\Installer\MSICC20.tmp, Entrypoint: WriteXMLServer SFXCA:
Extracting custom action to temporary directory:
C:\Windows\Installer\MSICC20.tmp-\ SFXCA: Binding to CLR version
v4.0.30319
The answer was very simple I forgot the
[CustomAction]
For the other two routines.

How to keep a config file when major upgrade in wix v3.8?

I want to keep a config file when the msi installer does a major upgrade. For the config file, I make a change when installing. The code is as follows:
<Component Id="MODIFYCONFIG" Guid="6A1D7762-B707-4084-A01F-6F936CC159CE" Win64="yes">
<File Id="Application.config" Name="Application.config" Vital="yes" KeyPath="yes" Source="Resource\Application.config"></File>
<util:XmlFile Id="SetValueIP" Action="setValue" Permanent="yes" File="[#Application.config]"
ElementPath="/configuration/applicationSettings/Application.Properties.Settings/setting[\[]#name='IpAddress'[\]]/value" Value="[IPADDRESS]" Sequence="1"/>
<util:XmlFile Id="SetValuePort" Action="setValue" Permanent="yes" File="[#Application.config]"
ElementPath="/configuration/applicationSettings/Application.Properties.Settings/setting[\[]#name='IpPort'[\]]/value" Value="[PORT]" Sequence="2"/>
<Condition>Not Installed</Condition>
</Component>
<Component Id="KEEPCONFIG" Guid="F7F173AA-C2FD-4017-BFBC-B81852A671E7" Win64="yes">
<RemoveFile Id="ApplicationConfig" Name="Application.config" On="uninstall"/>
<Condition>(REMOVE=ALL) AND (NOT UPGRADINGPRODUCTCODE)</Condition>
</Component>
But when a major upgrade occurs the file is not preserved. How can I preserve the modified file?
This solved it for me... config file is preserved with minor/major upgrade, and completely removed on uninstall.
Ref: Aaron Stebner: How to retain user-customized files during a Windows Installer major upgrade
EDIT: Summarized info from the linked page...
Each config file shall have it's own component, where the config file is marked as the keypath of the component. Unversioned file replacement logic will be used by the Windows Installer.
Add "RemoveExistingProducts" action after the "InstallFiles" action. New versions of all components are installed before removing the old MSI. When it's done in this sequence, the components will have their reference count incremented to 2, but the config files will not be replaced unless they are unmodified (because of unversioned file replacement logic). When the old MSI is removed, the reference count will be decremented back to 1, but the files will not be removed because the reference count are not 0.
You have 3 options when upgrading:
Make the config file component permanent. This will not un-install it, and you will be able to upgrade it, but removing it will be very difficult.
Use the Remember property pattern to store the config settings for the IP and PORT in the registry.
As part of the install, write the config file to a temporary filename and then use a CopyFile command to create the destination file. On upgrade check for the file using a FileSearch, and if it exists then don't copy. Only issue here is if the config file has changed you won't get the updated sections.
The best option is the remember me property as this has the least problems.
It took me a while, but here is how I solved it myself. It's probably a variation of caveman_dick's third option.
1) Add new action into UISequence to back up your current config file. You can do it with the magic of custom action and ComponentSearch to actually locate the file.
2) Restore the file later in ExecuteSequence.
<Binary Id="CustomActions.CA.dll" SourceFile="..\CustomActions\bin\$(var.Configuration)\CustomActions.CA.dll" />
<CustomAction Id="BackupConfigFile"
Return="check"
BinaryKey="CustomActions.CA.dll"
DllEntry="BackupFile" />
<CustomAction Id="RestoreConfigFile"
Return="check"
Execute="deferred"
Impersonate="no"
BinaryKey="CustomActions.CA.dll"
DllEntry="RestoreFile" />
<CustomAction Id="PropertyDelegator"
Property="RestoreConfigFile"
Value="MYTARGET=[MYTARGET];FILENAME_TO_BACKUP=[FILENAME_TO_BACKUP]" />
<Property Id="FILENAME_TO_BACKUP" Value="test.exe.config" />
<Property Id="PREVIOUS_PATH">
<ComponentSearch Id="evSearch" Guid="{010447A6-3330-41BB-8A7A-70D08ADB35E4}" />
</Property>
and here is quick CustomAction.cs I wrote:
[CustomAction]
public static ActionResult BackupFile(Session session)
{
try
{
// check out if the previous installation has our file included
// and if it does,
// then make copy of it.
var previousInstallationPath = session["PREVIOUS_PATH"];
var fileToBackup = session["FILENAME_TO_BACKUP"];
if (!string.IsNullOrEmpty(previousInstallationPath) && !string.IsNullOrEmpty(fileToBackup))
{
var absolutePath = Path.Combine(previousInstallationPath, fileToBackup);
if (File.Exists(absolutePath))
{
var destinationPath = Path.Combine(Path.GetTempPath(),
string.Concat(fileToBackup, _MODIFIER));
File.Copy(absolutePath, destinationPath);
}
}
}
catch (Exception e)
{
session.Log("Couldn't backup previous file: {0}", e);
}
return ActionResult.Success;
}
[CustomAction]
public static ActionResult RestoreFile(Session session)
{
try
{
// check if our CustomAction made backup of file,
// and if it indeed exists in temp path, then
// we basically copy it back.
var currentInstallationPath = session.CustomActionData["MYTARGET"];
var fileToRestore = session.CustomActionData["FILENAME_TO_BACKUP"];
var fileOriginalContentPath = Path.Combine(Path.GetTempPath(),
string.Concat(fileToRestore, _MODIFIER));
if (File.Exists(fileOriginalContentPath))
{
var destinationFile = Path.Combine(currentInstallationPath, fileToRestore);
if (File.Exists(destinationFile))
File.Delete(destinationFile);
File.Move(fileOriginalContentPath, destinationFile);
}
}
catch (Exception e)
{
session.Log("Couldn't restore previous file: {0}", e);
}
return ActionResult.Success;
}
to actually define sequences:
<InstallUISequence>
<Custom Action="BackupConfigFile" After="AppSearch"></Custom>
</InstallUISequence>
<InstallExecuteSequence>
<Custom Action="PropertyDelegator" Before="RestoreConfigFile" />
<Custom Action="RestoreConfigFile" After="InstallFiles"></Custom>
</InstallExecuteSequence>
haven't tested it thoroughly, but seems to do the job for now. Caveat: Temp folder might change?!
Alternatively there is this one that I found from Internet, but haven't tested it.
<!-- Support Upgrading the Product -->
<Upgrade Id="{B0FB80ED-249E-4946-87A2-08A5BCA36E7E}">
<UpgradeVersion Minimum="$(var.Version)"
OnlyDetect="yes" Property="NEWERVERSIONDETECTED" />
<UpgradeVersion Minimum="0.0.0"
Maximum="$(var.Version)" IncludeMinimum="yes"
IncludeMaximum="no"
Property="OLDERVERSIONBEINGUPGRADED" />
</Upgrade>
<Property Id="OLDERVERSIONBEINGUPGRADED" Secure="yes" />
<!-- Action to save and Restore the Config-File on reinstall
-->
<!-- We're using CAQuietExec to prevent DOS-Boxes from
popping up -->
<CustomAction Id="SetQtCmdLineCopy" Property="QtExecCmdLine"
Value=""[SystemFolder]cmd.exe" /c copy
"[INSTALLDIR]MyApp.exe.config"
"[INSTALLDIR]config.bak"" />
<CustomAction Id="QtCmdCopy" BinaryKey="WixCA"
DllEntry="CAQuietExec" Execute="immediate" />
<CustomAction Id="SetQtCmdLineRestore"
Property="QtCmdRestore" Value=""[SystemFolder]cmd.exe" /c move
/Y "[INSTALLDIR]config.bak"
"[INSTALLDIR]MyApp.exe.config"" />
<CustomAction Id="QtCmdRestore" Execute="commit"
BinaryKey="WixCA" DllEntry="CAQuietExec" />
<!-- These actions will run only for a major upgrade -->
<InstallExecuteSequence>
<Custom Action="SetQtCmdLineCopy"
After="InstallInitialize"> NOT (OLDERVERSIONBEINGUPGRADED = "")</Custom>
<Custom Action="QtCmdCopy"
After="SetQtCmdLineCopy">NOT (OLDERVERSIONBEINGUPGRADED = "")</Custom>
<Custom Action="SetQtCmdLineRestore"
Before="InstallFinalize">NOT (OLDERVERSIONBEINGUPGRADED = "")</Custom>
<Custom Action="QtCmdRestore"
After="SetQtCmdLineRestore">NOT (OLDERVERSIONBEINGUPGRADED =
"")</Custom>
</InstallExecuteSequence>
There is another option, but it may not be applicable to your scenario - it all depends on who is initially running your installer...
If your app is downloaded over the web for example, then we usually go with caveman_dick's remember property pattern.
However, we have a couple of suites of products that are always installed by our own installation staff who visit a clients site. In this instance, simply do not include the config file in the installer at all!
Put simply - if the installer doesn't know about a file, then it won't uninstall it!
In this case you have the option of your installation team creating and configuring the config file, or your app creating it when it doesn't exist, and asking the user for the values.
As stated this won't be an option in some scenarios, but it works fine for ours.
Add Schedule="afterInstallExecuteAgain" in the MajorUpgrade
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." Schedule="afterInstallExecuteAgain" />
It work for me

Custom property is empty

I have custom action which stores the file location into property at immediate phase.
Then I have another action at deffered phase, which reads this property and creates the file:
String configFilePath = session.CustomActionData["configPath"];
String configFileName = session.CustomActionData["configFile"];
...
Everything works so far.
At uninstall I would like to remove this file, so I have another deffered action, which executes after removing files. Action works, the problem is that property is empty(session.CustomActionData is empty).
Why? I have setted it at immediate phase also when uninstall is happening. And if I can read it at custom setUpConfig action, why I can't read it at custom removeConfig action?
I guess one option is to store this value into registry at installing phase and then read it from there when uninstalling. But I would like to know why i can set the property when installing the program, and why it is not setted at uninstalling. The action, which sets the proeprty is executed in both cases.
<CustomAction Id="ConfigFileLocation" Property="setUpConfig" Execute="immediate"
Value="configPath=[WEBSITE];configFile=config_template.asp" />
<CustomAction Id="setUpConfig" BinaryKey="MyCustomAction" DllEntry="configFile"
Execute="deferred" Impersonate="no" Return="check" />
<CustomAction Id="removeConfig" BinaryKey="MyCustomAction"
DllEntry="removeCustomFile" Execute="deferred" Impersonate="no" Return="check" />
<InstallExecuteSequence>
<Custom Action="ConfigFileLocation" After="LaunchConditions" />
<Custom Action="setUpConfig" Before="InstallFinalize"><![CDATA[NOT REMOVE="ALL"]] />
<Custom Action="removeConfig" After="RemoveFiles"><![CDATA[REMOVE="ALL"]] />
</InstallExecuteSequence>
To pass data to a deferred custom action you must set a Property with a name that matches the CustomAction/#Id. You are doing that correctly for the setUpConfig custom action via the immediate ConfigFileLocation custom action.
However, the removeConfig custom action does not have an immediate custom action setting a Property named removeConfig.
Ergo the removeConfig custom action's CustomActionData property is blank.

WiX unable to check property that is set using CustomAction

I want to include different dll in the installation based on certain values. So, I am trying to load a component based on a property that is set using custom action.
In wxs file:
...
<Property Id="PropDllVersion" Value="0" />
...
<CustomAction Id="CheckPropDllVersion" BinaryKey="CustomAction1.dll" DllEntry="GetPropVersion" Return="ignore" Execute="immediate"/>
...
<InstallExecuteSequence>
<Custom Action="CheckPropDllVersion" After="ValidateProductID" />
</InstallExecuteSequence>
...
<Component Id="Test"
Guid="B81F832D-2D96-4169-9BD0-8D77098FEC60">
<Condition><![CDATA[PropDllVersion = "19"]]></Condition>
<File Id="File15"
Name="xyz.dll"
Vital="yes"
KeyPath="yes"
AssemblyManifest="File5"
AssemblyApplication="File5"
Assembly=".net"
DiskId="1"
/>
</Component>
...
Then in the custom action file:
[CustomAction]
public static ActionResult GetPropVersion(Session session)
{
session["PropDllVersion"] = "19";
}
I can see in the msi log file that this property is changed to 19, however the xyz.dll is not included in the installation. It looks like the PropDllVersion is not set at the condition level or am I doing something wrong... I tried to sequence it at many other places still it is not working...
If I use a global property in the condition instead of my property it works!
Please note that private properties (its name contains lowercase letters) use their default values in InstallExecuteSequence. So you should use a public property, for example PROP_DLL_VERSION.
Where else have you tried to sequence your custom action? You need to sequence it before the CostInitialize action.
Also, call the FileCost action following CostInitialize and the CostFinalize action then.
You can check out the documentation of the CostInitialize action here:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa368050%28v=vs.85%29.aspx

can't pass properties to WiX custom action

I've read How do I pass msiexec properties to a WiX C# custom action? , but that didn't answer my question, or maybe I just don't see what I am doing wrong.
My install package fails to install, and the logs say that my property wasn't found in the custom actions collection. My code is:
<CustomAction Id="SetCustomActionDataValue" Return="check" Property="Itp.Configurator.WixCustomAction" Value="G=G2" />
<CustomAction Id="CreateDatabase" BinaryKey="Binary1" DllEntry="CreateDatabase" Execute="deferred" Return="check" />
<InstallExecuteSequence>
<Custom Action='SetCustomActionDataValue' After="InstallFiles"/>
<Custom Action='CreateDatabase' After="SetCustomActionDataValue">
NOT Installed AND NOT PATCH
</Custom>
</InstallExecuteSequence>
And code inside the custom action is:
string Property1 = session.CustomActionData["G"];
The name of the property in your first element must be exactly the same as the name of the deferred custom action you'd like to pass the value to. So, if the deferred action is CreateDatabase, then the first element should look like this:
<CustomAction Id="SetCustomActionDataValue" Return="check" Property="CreateDatabase" Value="G=G2" />