Preserving configuration via .NET Custom Action - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

NSentinel
Posts: 32
Joined: Mon Oct 27, 2008 10:09 am

Preserving configuration via .NET Custom Action

Hi,

I want to give our users way to configure our application during installation preserving existing user's settings (if any). We install .NET application and using standard config files (app.config).

So I created simple .NET application (to attach as .NET Custom Action later) which populates important values from existing users config during installation into public properties (as I understand early this is only way to read existing settings?). To test my installer class I attached it as .NET custom action to Install and Commit events and dump info to text files (for checking).

All goes successfully: info readed and populated into properties as I planned. So I decided to make dialogs to give users ability to configure populated values and store modified config. Here I get stuck...

First of all, .NET Custom Action always executed almost end of installation, so any dialogs I try to insert in sequence shown right before .NET Custom Action executes to populate values. I try different events (in InstallUISequence) but with same result.

I know that I can make dialog in Installer class.. but it looks weird (additional popup window), so if it possible I want to .NET CA do its job hidden.

And second, I planned to modify result config by using XML Editor feature. Is this right way?

What is standard way to do such configuration? Maybe I miss some guides or forum topics, or I try to do this in totally wrong way... please give me right direction.

Sincerely yours,
Nick
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Preserving configuration via .NET Custom Action

Hi Nick,
So I created simple .NET application (to attach as .NET Custom Action later) which populates important values from existing users config during installation into public properties
I'm afraid that .NET Installer Class actions cannot set installer properties. However, you can try using the MsiSetProperty function in a C++ DLL custom action. This custom action can be scheduled in the beginning of the install, for example under "InstallUISequence" -> "Begin".
(as I understand early this is only way to read existing settings?)
Yes, existing files can be read only through a custom action.
And second, I planned to modify result config by using XML Editor feature. Is this right way?
Yes, if the target file uses an XML format you can import it as an XML in your project and modify it through the XML editor.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
NSentinel
Posts: 32
Joined: Mon Oct 27, 2008 10:09 am

Re: Preserving configuration via .NET Custom Action

cosmin wrote:
I'm afraid that .NET Installer Class actions cannot set installer properties. However, you can try using the MsiSetProperty function in a C++ DLL custom action. This custom action can be scheduled in the beginning of the install, for example under "InstallUISequence" -> "Begin".
Thank you for response. I digg around a bit and found same...

I want .NET Custom action just for simplify XML parsing. Maybe there are better (ready to use) solution written in VB Script or similar to read, parse and populate properties from XML file, so I can avoid .NET?
NSentinel
Posts: 32
Joined: Mon Oct 27, 2008 10:09 am

Re: Preserving configuration via .NET Custom Action

I found extended answer on my start question in this topic. Hope it helps someone else:
I am writing a custom action handler in C#, using the .NET Installer class. I would like to use the MsiGetProperty() method to get the values of some installer properties when the custom action is run (for example, product code and name). Unfortunately, the .NET Installer class appears to "hide" the MSIHANDLE. Is there another way to get to the MSIHANDLE?

For various reasons, I am trying to avoid using the custom-action-data mechanism for passing installer property info.
Phil Wilson wrote:Not in a managed custom action, no. The MsiHandle doesn't get passed through. The most straightforward way is just to write a C++ custom action Dll function. These work fine and get passed the handle that you can use directly on MsiGetProperty etc.

The installer class doesn't so much hide it - it never gets it. MSI calls a C++ custom action function called ManagedInstall in InstallUtilLib, packaged in your MSI file. The code there gets the CustomActionData as well as the location of your assembly, and then uses reflection to load your assembly and instantiate your installer class. My attitude is that although managed custom actions are implemented with an interesting mechanism, custom actions are specialized pieces of code that get called from Windows, somewhat like device drivers, and the native way to use them is probably best, and that means C++.

Natively, Windows Installer supports custom actions in form of a DLL, an executable, VBScript or JScript. DLL here means a "real" DLL that exports callable functions, typically written in C++. You cannot create such a DLL in Visual Basic.

Technically it is possible to use assemblies as custom actions (although not natively) but in the past this has been discouraged by many experts for the following reasons:
  • Functionally they're not as rich. You can pass properties into them, but you can't create property values to return into the running installation.
  • They often get used when not needed. For example, Visual Studio setups encourage Installer classes and associated code to install Services despite perfectly good support in Windows Installer with standard tables and actions.
  • There are bear traps, such as calling a managed custom action that has a dependency on an assembly you've just installed in the GAC. Unfortunately assemblies aren't actually in the GAC until InstallFinalize, so your custom action fails.
  • When they fail, they do so in mysterious ways, mainly because of the black box shim Dll between Windows Installer and your code (InstallUtilLib.Dll). How obvious would it be that "Unknown error in CorBindToRuntimeHost..." is caused by the config file string being in the wrong place? Installation errors can be pretty obscure already, and there's no need to add to that situation! The shim Dll is effectively hiding what's going on, and AFAIK it's undocumented.
  • InstallUtil does not integrate properly with the Windows Installer and thus reference counting is not done properly.

Phil Wilson
[Microsoft MVP-Windows Installer]
Original posts:
How do I get an MSIHANLDE for use during a custom action
How can I create Custom Actions in Managed Languages, like C# ?

Additional info: Don't use managed code to write your custom actions!
Don't use vbscript/jscript to write your custom actions!

So best way seems to use C++.

Return to “Common Problems”