Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

Custom actions allow you to specify additional actions to be performed on the target computer at the end of an installation. For example, in this walkthrough, you pass the path name of a DLL to the CustomActionData property in order to precompile the assembly to native code during installation.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

NoteNote

The following instructions demonstrate creating a deployment project using a Visual Basic project; the general principles apply to all Visual Studio language projects that support deploying Windows applications.

To create the custom action class

  1. On the File menu, click 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. On the Project menu, choose Add New Item.

  4. In the Add New Item dialog box, choose Installer Class. In the Name box, type InstallClass.vb. Click Add to add the class to the project.

To add code to the custom action

  1. Right-click InstallClass.vb in Solution Explorer (or the design surface) and click View Code to open the Code Editor. Add the following code to the top of the module, below Imports System.Configuration.Install:

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    
  2. In InstallClass.vb, add the following declaration below Public Class InstallClass:

    Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" _
      (<Runtime.InteropServices.MarshalAs( _
      System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
      ByVal Buffer As System.Text.StringBuilder, _
      ByVal BufferLength As Integer, ByRef Length As Integer) As Integer
    
    
  3. In InstallClass.vb, after the End Sub statement for MyBase.New, add the following procedure to override the Install procedure of the base class:

    <Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)> _
    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, and then click New Project.

  2. In the Add New 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. In the Properties window, select the ProductName property and type InstallClass.

  4. In the File System Editor, select the Application Folder. On the Action menu, choose Add, and then click Project Output.

  5. 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 the click 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, then select Primary output from InstallClass (Active). Click OK to add the InstallClass custom action to all four custom action nodes.

  4. Select the Primary output from InstallClass (Active) node beneath the Install node.

  5. In the Properties window, select the CustomActionData property and type /Args="[TARGETDIR]InstallClass.dll". Include the quotation marks.

  6. Repeat for each of the other three instances of Primary output from InstallClass (Active).

  7. 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.

    NoteNote

    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\My Documents\Visual Studio 2005\Projects\Solution Folder Name\My Notepad Installer\project configuration\My Notepad Installer.msi. The default project configuration is Debug or Release.

  2. Copy InstallClass Installer.msi, Setup.exe, and all other files and subdirectories in the directory to another computer.

    NoteNote

    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 Setup.exe to run the installer.

    NoteNote

    You must have install permissions on the computer in order to run the installer.

To test the installation

  • Navigate to the assembly folder %windir%\assembly (for example, C:\Windows\assembly) and find InstallClass. In the Type column, its type should be marked as Native Images.

To uninstall the application

  1. In Windows Control Panel, double-click Add or Remove Programs.

  2. In the Add or Remove Programs dialog box, select InstallClass Installer and click Remove, and then click OK to close the dialog box.

    NoteTip

    To uninstall from your development computer, on the Project menu, choose Uninstall.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft