Geeks With Blogs
Bret Shawn

Custom Actions can be a simple way to add needed UI elements to the Visual Studio Setup Project, without having to get your hands dirty.  However, Windows Vista and Windows 7 push those Custom Action forms into the background of your installer package, which can be a neusance for the person performing the install.

I found this code posted on the MSDN Community Forums and loved its simplicity.  I have refactored a bit, but full credit goes to user; Funday for  this approach.  You'll find a link to the original thread posting at the end of this article. 

Now, let's get to it!  First, add the following class to your custom action project. 


     public class WindowHandleWrapper : IWin32Window
    {
 
        public IntPtr Handle { get; private set; }
 
        public WindowHandleWrapper(IntPtr handle)
        {
            this.Handle = handle;
        }
 
        public WindowHandleWrapper(string installerWindowTitle)
            : this(GetWindowHandle(installerWindowTitle)) { }
 
        public static IntPtr GetWindowHandle(string installerWindowTitle)
        {
            try
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName.Equals("msiexec",
StringComparison.OrdinalIgnoreCase)
                        && process.MainWindowTitle.Equals(installerWindowTitle, StringComparison.OrdinalIgnoreCase))
                    {
                        return process.MainWindowHandle;
                    }
                }

                // If we made it this far, the process was not found.
                throw new ArgumentException("Unable to create WindowHandleWrapper.  Window '" + installerWindowTitle + "' not found.", "installerWindowTitle");
            }
            catch
            {
                throw;
            }
        }
    }

 


Next, add a new installer class to the project you want to create the custom action from. (Right-click project, Add New, InstallerClass).  Use the code in the following the Commit() method as a guide to creating an instance of the above class and opening the window.


 
    [RunInstaller(true)]
    public partial class UpgradeInstaller : Installer
    {
        public UpgradeInstaller()
        {
            InitializeComponent();
        }
 
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
 
            WindowsInstallerForm wiForm = new WindowsInstallerForm();
 
            wiForm.ShowDialog(new WindowHandleWrapper(Context.Parameters["productName"]));
        }
    }


Lastly, in your installer project’s Custom Action Editor, add the output of your custom action project and set its CustomActionData property to the following:
 
/productName="[ProductName]"
 
 
That should do it!

Here's a link to Funday's post:
http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/158ad5bd-d3f7-4a21-8ebe-341e9741810a

 

Posted on Friday, February 19, 2010 8:34 AM | Back to top


Comments on this post: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Very nice article. Thanks a lot
Left by Beemen Beshara on Feb 22, 2010 11:52 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Thanks for the feedback!
Left by Bret Shawn on Feb 22, 2010 2:15 PM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Wow, works like a charm! Thanks a lot!!
Left by nikneem on Jan 10, 2011 3:08 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
I tried your technique but the process.MainWindowTitle is always empty, and the MainWindowHandle is 0 for all of the enumerated processes, perhaps this a permissions issue?
Left by Cary Lewis on Jul 07, 2011 10:24 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Finally an answer to the problem. You and Funday are brilliant!
When creating the WindowHandleWrapper class you will need to add the following using statements:
using System.Diagnostics;
using System.Windows.Forms;
Left by Formerly frustrated on Jan 11, 2012 1:54 PM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Hey man the code is really awesome buddy it helped me alot alot alot thanks qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
Left by Nayeem on Feb 13, 2012 12:34 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
I am getting assembly/namespace couldnot find error for the WindowsInstallerForm. I searched for this assembly but i could not find this.

line is WindowsInstallerForm wiForm = new WindowsInstallerForm();
Left by ROSE on Nov 28, 2012 6:50 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Rose,

Very good question. The WindowsInstallerForm was the form that I wanted to display. Just reference your own form in place of that line.

Left by Bret Shawn on Nov 29, 2012 12:05 PM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Thanks a lot... This saved my life.... Very nice answer...
Left by Vidya Anand on Jun 19, 2013 2:20 AM

# re: How to: force a Windows Installer Custom Action form to show on top in Windows Vista / 7
Requesting Gravatar...
Thanks alot this solved my problem!!
Left by Peter on Jul 29, 2013 9:09 AM

Your comment:
 (will show your gravatar)


Copyright © Bretshawn | Powered by: GeeksWithBlogs.net