run macro / exe during installation - msi installer

tjoseph833
tjoseph833 used Ask the Experts™
on
Can we run macros or exe before program installation in installer? Can we check and delete windows folders  (basically to cleanup before the actual installation)?

Am creating the setup installer with standard Visual Studio 'Setup and Deployment'. I have to run some macros to check older software entries. When the user clicks setup.exe ... this cleanup macro has to run. Is it possible?

Any help or ideas is much appreciated. Cheers.
 

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
fridomCEO/Programmer

Commented:
Yes that can be done, this actions are called custom actions.
See e.g http://msdn.microsoft.com/en-us/library/aa368066%28VS.85%29.aspx

Regards
Friedrich
Meir RivkinFull stack Software Engineer

Commented:
by macros do u mean script?
Meir RivkinFull stack Software Engineer

Commented:
which Visual studio you use?

Author

Commented:
hi ... am using VS 2008. Its a registry cleanup script. earlier version of this software wasn't installed using msi installer but via batch script. its an old VB6 software and I need to remove the program folder too before the new .NET software is installed.



Meir RivkinFull stack Software Engineer
Commented:
to do that u need to use custom action.
please follow this steps:
the solution u created should contain 2 projects:
Setup deployment project and class library project.
the class library will contain the custom action code which will be invoked during the installation process.
basically, u bind the dll to the installer by override installation API like OnBeforeInstall, OnAfterInstall etc.

i assume u already created the setup project so i'll walkthrough with you the steps require to bind the dll to the installer.

1. add new project class library.
2. add reference System.Configuration.Install.
3. the class library code should look like this:

using System.Configuration.Install;
using System.ComponentModel;

 [RunInstaller(true)]
    public class CustomInstaller:Installer
    {
        protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
        {
            //your custom code goes here...
            base.OnBeforeInstall(savedState);
        }
    }

now u can add your custom code to the place i've marked above (running your registry script)

Next step is to bind the class lib to installer project.
4. right-click on setup installer ->Add ->Project Output (see screenshot #1)
5. in the project window choose the class library you just created (see screenshot #2)
6. right-click on setup installer ->View ->Custom Actions (see screenshot #3)
you gonna see Custom Actions as the root node and the following actions as child nodes: Install,Commit,Rollback,Uninstall
for each one u can add custom action.
7. right click Custom Actions root node ->Add Custom Action -> dbl-click Applciation Folder ->Choose "Primary output From ..."
8. rebuild the solution and u ready to go.

to make sure the dll is binded correctly to the installer, u can insert Debugger.Break in the custom action code:
[RunInstaller(true)]
    public class CustomInstaller:Installer
    {
        protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
        {
            Debugger.Break();
            //your custom code goes here....
            base.OnBeforeInstall(savedState);
        }
    }
      
      Run the installer, if the code breaks in the debugger line, means its all good.
      the only thing left for you to do is add your code to run the registry script.




screenshot--1.jpg
screenshot--2.jpg
screenshot--3.jpg

Author

Commented:
Hi ... am facing another problem and been working on this whole day without success. The OnBeforeInstall event is raised AFTER all the new files have been copied and registered. I want the cleanup to run BEFORE the new install and file copy. Is there no way to do this in Visual Studio 2008?

 
Full stack Software Engineer
Commented:
the way MSI infrastructure works is first deploy the files and then do the rest, so if u wish to run some cleanup before even extracting the Msi package, u have few alternatives:
1. extract the files to a backup folder other this the desired installation directory, then perform your cleanup and then copy the binaries to the real installation directory.
2. have a nested msi installers, the outer installer (container) package will contain the actual installer plus an executable/script which will be run by the container installer.
the executable/script will first wait till the container msi finish the installation (since 2 MSI instances cannot run on the same time), then run the cleanup, then install the inner MSI package and finally uninstall the Msi
container.

option #2 sounds complicated and i actually implemented this in my company to support pre/post installation procedures to have the ability to run scripts before installation and after installation (and they can't be part of the Msi package itself).

Author

Commented:
thanks for the help. I think better create another exe to clean up manually. its way too crazy to develop anything advance using VS setup installer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial