Walkthrough: Using a Custom Action to Pre-Compile an Assembly During Installation

The following walkthrough demonstrates the use of a custom action and the CustomActionData property to precompile an assembly to native code during installation.

To create the custom action class

  1. On the File menu, choose New, Project.
  2. In the New Project dialog box, select Visual Basic Projects in the Project Type pane, and then choose Class Library in the Templates pane. In the Name box, type InstallClass.
  3. Click OK to close the dialog box.
  4. On the Project menu, choose Add New Item.
  5. In the Add New Item dialog box, choose Installer Class. In the Name box, type InstallClass.
  6. Click OK to close the dialog box.

To add code to the custom action

  1. Double-click the InstallerClass.vb designer to open the Code Editor. Add the following code to the top of the module, beneath Imports System.Configuration.Install:
    Imports System.Runtime.InteropServices
    Imports System.Text
    
  2. Add the following declaration beneath Inherits System.Configuration.Install.Installer:
    Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" (<MarshalAs(UnmanagedType.LPWStr)> ByVal Buffer As System.Text.StringBuilder, ByVal BufferLength As Integer, ByRef Length As Integer) As Integer
    
  3. Beneath Component Designer generated code, add the following procedure to override the Install procedure of the base class:
    Public Overrides Sub Install(ByVal savedState As System.Collections.IDictionary)
       MyBase.Install(savedState)
       Dim Args As String = Me.Context.Parameters.Item("Args")
    
       If Args = "" Then
          Throw New InstallException("No arguments specified")
       End If
    
       ' Gets the path to the Framework directory.
       Dim Path As New System.Text.StringBuilder(1024)
       Dim Size As Integer
       GetCORSystemDirectory(Path, Path.Capacity, Size)
    
       Dim P As Process
       ' Quotes the arguments, in case they have a space in them.
       Dim Si As New ProcessStartInfo(Path.ToString() & "ngen.exe", Chr(34) & Args & Chr(34))
       Si.WindowStyle = ProcessWindowStyle.Hidden 
       Try
          P = Process.Start(Si)
          P.WaitForExit()
       Catch e As Exception
          Throw New InstallException(e.Message)
       End Try
    End Sub
    

To add a deployment project

  1. On the File menu, choose Add Project, New Project.
  2. In the Add Project dialog box, select Setup and Deployment Projects in the Project Type pane, and then choose Setup Project in the Templates pane. In the Name box, type InstallClass Installer.
  3. Click OK to close the dialog box.
  4. In the Properties window, select the ProductName property and type InstallClass.
  5. In the File System Editor, select the Application Folder. On the Action menu, choose Add, Project Output.
  6. In the Add Project Output Group dialog box, select the primary output for the InstallClass project, then click OK to close the dialog box.

To add the custom action

  1. Select the InstallClass Installer project in Solution Explorer. On the View menu, point to Editor, and choose Custom Actions.
  2. In the Custom Actions Editor, select the Custom Actions node. On the Action menu, choose Add Custom Action.
  3. In the Select item in project dialog box, double-click the Application Folder.
  4. Select the Primary output from InstallClass (Active) item, then click OK to close the dialog box.

    This will add the InstallClass custom action to all four custom action nodes.

  5. Select the Primary output from InstallClass (Active) node beneath the Install node.
  6. In the Properties window, select the CustomActionData property and type /Args="[TARGETDIR]InstallClass.dll". Include the quotes.
  7. Repeat for each of the other three instances of Primary output from InstallClass (Active).
  8. On the Build menu, choose Build InstallClass Installer.

To install on your development computer

  • Select the InstallClass Installer project in Solution Explorer. On the Project menu, choose Install.
    Note   You must have install permissions on the computer in order to run the installer.

To deploy to another computer

  1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be \documents and settings\yourloginname\InstallClass Installer\project configuration\InstallClass Installer.msi. The default project configuration is Debug.
  2. Copy the InstallClass Installer.msi file and all other files and subdirectories in the directory to another computer.
    Note   To install on a computer that is not on a network, copy the files to traditional media such as CD-ROM.
  3. On the target computer, double-click the Setup.exe file to run the installer.
    Note   You must have install permissions on the computer in order to run the installer.

To test the installation

  • Navigate to the assembly folder (located beneath the Windows folder — usually C:\Winnt\assembly) and find InstallClass. In the Type column, its type should be marked as Native Images.

To uninstall the application

  1. In the Windows Control Panel, double-click Add/Remove Programs.
  2. In the Add/Remove Programs dialog box, select InstallClass Installer and click Remove, then click OK to close the dialog box.
    Tip   To uninstall from your development computer, on the Project menu, choose Uninstall.

See Also

Custom Actions Management in Deployment | CustomActionData Property | Compiling MSIL to Native Code | Native Image Generator (Ngen.exe)

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft