The .NET Developer Community

Custom Actions - Part 2

rated by 0 users
This post has 0 Replies | 1 Follower

rock
Top 25 Contributor
Sydney, Australia
Since 5/10/2003
Posts 4,853
Reputation 35,034
The first part of this FAQ document can be read at: Custom Actions - Part 1

It also has the code to deploy some components without executing their installation scripts, this is done on purpose, as We don't want to install anything on the machine you are testing this FAQ's project; beside We are not including their installation packages to keep the attachment size small.

You will find the folder Cd_Image inside the DP_CA folder; this folder is not part of the deployment package, it is there to build an image of the distribution media because the project includes files and third party packages installed by one of its custom actions, you have to copy all the files on the Debug or Release folders here every time you amend your deployment project.

Some of this document code samples are included in the attached zip file, under the DP_CA folder, this is done to keep the size of the document small, and you will find the word document Custom_Actions_FAQ.doc in the same folder. It contains the uncut version of this document.

CUSTOM ACTION CODE TO PARSE ITS PARAMETERS

You need minimum two lines of code to parse a custom action parameters list, this code is located in the Form1_Load event, as shown below

Code:

   Dim f() As String = System.Environment.GetCommandLineArgs
   Dim sAction As String = f(1).ToLower

The array f() contains all the parameters expected by the custom action, f(0) contains the win application name hosting the custom action code, f(1) the action taking place in the deployment project (install, uninstall, commit and rollback). sAction receive its value and convert it to lower case.

Down the code in your Form1_Load event you may have a case statement splitting the logic based on the installation stage the deployment project is executing:

Code:

   Select Case sAction
      Case "i" ' installation
      Case "u" ' uninstall
      Case "r" ' rollback
      Case "c" ' commit
   End Select

CUSTOM ACTION CODE TO CREATE A SHORTCUT

Shortcuts are created by the WinShell object, your custom action application should contain a reference to this object, located under the References COM tab under Windows Script Host Object Model, Imports the IWshRuntimeLibrary object once you add this reference, the code window below illustrate the latest:

Code:

Imports IWshRuntimeLibrary

Public Class Form1

Assuming the user selection to create the shortcut is passed as the second parameter to the custom action (INSTALL_SC in the project), the next lines of code are required in your Form1_Load event:

Code:

   Dim sCreate_SC As String = f(2).ToLower
   Dim b As Boolean

The select statement sorting out the installation stage will contain these lines at the Case "i" block:

Code:

Select Case sAction
   Case "i" ' Installation
      If sCreate_SC.Length = 0 Then
         b = CreateShortCutOnDesktop(f(5), f(4) & "Hello_World.exe", "", "")
      End If

Above, the fifth parameters should contain the system folder to create the shortcut on, and the fourth parameter refers to the location of your program on the target machine.

Finally the CreateShortCutOnDesktop function:

Code:

Public Function CreateShortCutOnDesktop(ByVal TargetFolder As String, _
                                        ByVal TargetFile As String, _
                                        ByVal userID As String, _
                                        ByVal passWord As String) As Boolean

   Try

      Dim DesktopDir As String = TargetFolder
      Dim shortCut As IWshRuntimeLibrary.IWshShortcut
      Dim WshShell As New WshShellClass

      ' short cut files have a .lnk extension
      shortCut = WshShell.CreateShortcut(DesktopDir & "MyNewShortcut.lnk")

      ' set the shortcut properties
      With shortCut
         .TargetPath = TargetFile
         ' System.Reflection.Assembly.GetExecutingAssembly.Location()
         .WindowStyle = 1
         .Description = "Shotcut DEMO"
         .WorkingDirectory = DesktopDir
         '
         ' the next line gets the first Icon from the executing program
         '
         '.IconLocation = _
         ' System.Reflection.Assembly.GetExecutingAssembly.Location() & _
         ' ", 0"
         '
         ' the next line sets the userID and passWord into the shortcut
         ' as arguments which will be read from the command line.
         '
         '.Arguments = userID & ", " & passWord
         '
         .Save() ' save the shortcut file
      End With
      Return True
   Catch ex As System.Exception
      ' add your error handling here, if any
      MessageBox.Show("We got an error creating the shortcut" & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

      Return False
   End Try
End Function

CUSTOM ACTION CODE TO EXECUTE AN INSTALLATION PACKAGE

The package installation program should be anywhere on your deployment media, it will be always nice to have it under a folder that identifies the package's provider (Microsoft, IBM_Lotus, Flash, etc).

The code below install the Microsoft Data Access Component on the target machine, it expect the installation program to be located at the Microsoft folder on the distribution media.

The variable bInstall_MDAC should be initialized by a function that checks the target machine's registry for the component entries (remember, each package provider has their own peculiar entries in the registry). You may not know the exact entries, so the propose function may return True until your work out

Code:

'
' Install MDAC
'
' Declare the required variables.
' + fSrc is taken from the third parameter received [SourceDir] property
' + bInstall_MDAC default to true, but should add a boolean function
' that search the MDAC registry entries in the registry.
'
Dim fSrc As String = f(3)
Dim bInstall_MDAC As Boolean = True
'
' Label8 in the code below keep the user up to date with the events taken
' place in the block below
'
Select Case sAction
   Case "i" ' installation
      If bInstall_MDAC = True Then
         '
         Label8.Text = "Installing MDAC's ODBC drivers ..."
         Application.DoEvents()
         '
         ' executing the MDAC installation package from the CD's
         ' Microsoft folder
         '
         rtProc = Process.Start(fSrc & "Microsoft\MDAC_Typ.exe", "/q")
         '
         Do While rtProc.HasExited = False
            '
            ' waiting for the package to install
            '
         Loop
         '
         Label8.Text = "MDAC's ODBC drivers installed."
      Else
         Label8.Text = "MDAC's ODBC drivers no required."
         '
      End If

   Case "u" ' uninstall section
      '
End Select

The previous code defaults bInstall_MDAC to true, but you can implement a function, like the one below, to check the registry for the MDAC on the target machine. You should take it as an example to help you implement other packages validations.

Registry handling relies on the System.Win32 namespace; you should import it at the beginninig of your class, just before its declaration.

Code:

The code illustrating the Install Files method is included in the attached project in the file: DP_CA\CA_INSTALLATION_PACKAGE.TXT

CUSTOM ACTION CODE TO INSTALL FILES

The following function copies all the files to a target folder (wTgt) from a source folder (wSrc), your custom action should receive these folders from the deployment project, the properties [SourceDir] and [TARGETDIR] contain them.

The function is based on the System.IO namespace, so you should import it before the custom action class declaration.

The function Do_Install_Graphics_Files as implemented below expect the Graphics folder on the target machine, you should add it under the Application Folder at the File System on Target Machine tree, setting its AlwaysCreate property to true.

Code:

The code illustrating the Install Files method is included in the attached project in the file: DP_CA\CA_INSTALL_FILES.TXT

SUMMARY

Custom Actions are application programs that you develop to perform installation shores outside the MSI installation package.

There are two types of Custom Actions; Installer Class and Win Application custom actions; their main difference is on how they receive their parameters.

Win Applications custom actions read their parameters from the Arguments property with some parsing required while Installer Classes pick them from the CustomActionData property, a keyword-value style of parameters.

Custom Actions execute near the end of the installation process.

Multiple Custom Actions execute in the sequence you enter them in your deployment project.

If your custom actions crash with a non trapped error, the whole installation process is reversed.

If the custom actions install files or packages outside the deployment project's MSI file, you should use a folder to build the deployment media other than the Debug or Release folders; the reason for this is that the Project's Rebuild wipe out anything on those folders. You will find the folder CD_Image in the zip file attached to this document.

REFERENCES
Custom Actions Management in Deployment
How To Create Shortcuts for a .NET Deployment Project

 

Idea Rock (Arnaldo Sandoval), former Microsoft MVP Visual Developer

"One can't possibly test everything" (Albert Einstein)

Articles and Resources This page contains links to several articles collected over time here at VbCity

Page 1 of 1 (1 items) | RSS
Copyright 1998-2017 vbCity.com LLC