[wix-users] how to pass multiple arguments to C# custom action

Brian Enderle brianke at gmail.com
Fri Apr 29 04:06:17 PDT 2016


I don't know for sure, but my guess is your collection does not have any
items in it.  Can you add a log statement and get the number of elements in
'values' (i.e., 'values.Count()').


Here is an example of setting up parameters in WiX and then using them in
the CustomAction by parameter name:

SQLSERVER, SQLUSER, SQLPASSWORD, and INSTALL_SCRIPTS_FOLDER are variables
captured by installation setup screen.


<Fragment>

    <!-- Setup SQL User credentials -->
    <util:User Id="SQLUser" Name="[SQLUSER]" Password="[SQLPASSWORD]" />

    <!-- Need to perform a CustomAction to execute SQL command script with
parameters -->
    <CustomAction Id="ExecuteSQLParameters"
                  Return="check"
                  Property="ExecuteSQL"

Value="Server=[SQLSERVER];Database=master;Username=[SQLUSER];Password=[SQLPASSWORD];Directory=[INSTALL_SCRIPTS_FOLDER]"
/>

    <CustomAction Id="ExecuteSQL"
                  BinaryKey="CustomAction.CA"
                  DllEntry="ExecuteSQLCustomAction"
                  Execute="deferred"
                  Impersonate="no"
                  Return="check" />

</Fragment>




CustomAction method:


        [CustomAction]
        public static ActionResult ExecuteSQLCustomAction(Session session)
        {
            // Get SQL credentials
           Models.Database _dbInfo = new Models.Database();
            _dbInfo.ServerName = session.CustomActionData["Server"];
            _dbInfo.DatabaseName = session.CustomActionData["Database"];
            _dbInfo.Username = session.CustomActionData["Username"];
            _dbInfo.Password = session.CustomActionData["Password"];
            _dbInfo.IntegratedSecurity = false;
            session.Log(String.Format("*** _dbInfo: {0}, {1}",
_dbInfo.ServerName, _dbInfo.Username));
        }

Brian

If you can't explain it simply, you don't understand it well enough.  -
Albert Einstein

On Fri, Apr 29, 2016 at 1:07 AM, Ian Bell <ib at ianbellsoftware.com> wrote:

> I am following an example in Nick Ramirez's book Wix 3.6 on how to pass
> multiple arguments to a C# custom action.   I can successfully pass a
> single
> argument but not multiple argument.    The end of log file shows
> 'myDeferredCA' property is correctly defined but
> 'session.CustomActionData.Values'  is always empty in my custom action.   I
> am obviously missing something simple and am hoping someone here can spot
> my
> 'hiccup'.   The 'session.CustomActionData.Values' should appear between the
> log entries 'Begin myDeferredCA' and 'End myDeferredCA' but the values
> 'abc123', 'def567' and 'ghi890' never appear between these two log entries.
> Can anyone spot what I am doing wrong?
>
> ThanX in advance.
>
> Ian
>
>
>
>
>
> CustomAction.cs
> ======================================================================
> namespace CustomAction1
> {
>         public class CustomActions
>         {
>                 [CustomAction]
>                 public static ActionResult myDeferredCA( Session session )
>                 {
>                         //Debugger.Launch();
>                         session.Log( "Begin myDeferredCA" );
>
>                         ICollection<string> values =
> session.CustomActionData.Values;
>                         foreach( var value in values ) {
>                                 session.Log( "value = {0}", value );
>                         }
>
>                         session.Log( "End myDeferredCA" );
>                         return ActionResult.Success;
>                 }
>         }
> }
>
>
>
> Product.wxs
> ======================================================================
> <?xml version="1.0" encoding="UTF-8"?>
> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
>
>   <Product Id="*" Name="_MySetupProject" Language="1033" Version="1.0.0.0"
> Manufacturer="MyCompany"
> UpgradeCode="b0d199cd-d214-4e68-95c0-ecf3aa2d0b0d">
>     <Package InstallerVersion="200" Compressed="yes"
> InstallScope="perMachine" />
>     <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName]
> is
> already installed." />
>     <MediaTemplate EmbedCab="yes"/>
>
>     <Property Id="PROP1" Value="abc123" />
>     <Property Id="PROP2" Value="def567" />
>     <Property Id="PROP3" Value="ghi890" />
>     <CustomAction Id="CustomActionId_SetProperties"
>                   Property="myDeferredCA"
>                   Value="Prop1=[PROP1];Prop2=[PROP2];Prop3=[PROP3]" />
>
>     <Binary Id="BinaryId_MyCustomActionsDLL"
>
>
> SourceFile="$(var.CustomAction1.TargetDir)$(var.CustomAction1.TargetName).ca
> .dll" />
>     <CustomAction Id="CustomActionId_MyCustomAction"
>                   BinaryKey="BinaryId_MyCustomActionsDLL"
>                   DllEntry="myDeferredCA"
>                   Execute="deferred"
>                   Return="check"/>
>
>     <InstallExecuteSequence>
>       <Custom Action="CustomActionId_SetProperties"
> Before="CustomActionId_MyCustomAction" />
>       <Custom Action="CustomActionId_MyCustomAction"
> After="InstallInitialize" />
>     </InstallExecuteSequence>
>
>     <Feature Id="ProductFeature" Title="My Setup Project 01" Level="1">
>       <ComponentGroupRef Id="ComponentGroupId_ProgramFilesFolder" />
>     </Feature>
>   </Product>
>
>   <Fragment>
>     <Directory Id="TARGETDIR" Name="SourceDir">
>       <Directory Id="ProgramFilesFolder">
>         <Directory Id="DirectoryId_ProgramFilesFolder" Name="MyCompany" />
>       </Directory>
>     </Directory>
>   </Fragment>
>
>   <Fragment>
>     <ComponentGroup Id="ComponentGroupId_ProgramFilesFolder"
> Directory="DirectoryId_ProgramFilesFolder">
>       <Component Id="ComponentId_MyApplication" Guid="*" >
>         <File Id="FileId_MyApplication"
> Source="$(var.MyApplication.TargetPath)" />
>       </Component>
>     </ComponentGroup>
>   </Fragment>
>
> </Wix>
>
>
>
> MySetuProject.log
> ======================================================================
> Calling custom action
> CustomAction1!CustomAction1.CustomActions.myDeferredCA
> Begin myDeferredCA
> End myDeferredCA
> MSI (s) (F0:90) [00:57:42:380]: Executing op:
> ActionStart(Name=ProcessComponents,Description=Updating component
>
>
>
> ____________________________________________________________________
> WiX Toolset Users Mailing List provided by FireGiant
> http://www.firegiant.com/
>


More information about the wix-users mailing list