Dela via


Creating In-Process Hosts

The in-process host is an add-in model that enables you to extend the Visual Studio Tools for Applications project system. You can use an in-process host to customize the project behavior of the integrated development environment (IDE) of either Visual Studio or Visual Studio Tools for Applications. For example, you might want to display a designer when the user double-clicks a project in Project Explorer.

You create in-process hosts as part of a Visual Studio project template. For more information, see Creating Project Templates (Visual Studio Tools for Applications).

About the In-Process Host

The in-process host provides a way for you to customize the add-in developer's design-time experience. You can automate the IDE using Visual Studio automation (DTE). Components you can automate include the project creation wizard, the visual designer, menus, and windows such as Project Explorer.

The in-process host runs in the same process as the IDE. The lifetime of the in-process host is equal to the lifetime of the project. An in-process host is an add-in for the project system that loads when the project is opened and unloads when the project is closed.

Communication between the in-process host and the IDE occurs through the HostAdapter. The in-process host can interact with the project and project items through the DTE property of the HostAdapter.

Accessing Custom Properties

The HostAdapter can access name/value pairs in the HostProperties section of the project file. For example, you might want to store the application path or the add-in path in custom properties so they are easy to update. For more information about the HostProperties section, see Creating Project Templates (Visual Studio Tools for Applications).

Use the GetProperty and SetProperty methods to add and retrieve the name or value of a stored property. The properties must have unique names; otherwise Visual Studio Tools for Applications throws a DuplicateHostPropertyException.

Implementing an In-Process Host

This section gives an overview of the tasks you must complete to implement an in-process host. For a step-by-step example, see Walkthrough: Creating and Updating an In-Process Host.

Complete the following tasks to implement an in-process host for your project template (each task is explained in the following sections):

  • Create an in-process host class, implementing the IInProcessHost interface for your project template.

  • Add the in-process host assembly to the global assembly cache.

  • Add a registry key for the in-process host.

  • Add the in-process host to a project template.

Creating an In-Process Host

To create an in-process host, you must create a class that implements the IInProcessHost interface. You must then implement the SetAdapter method in your class, as the following example illustrates.

public class IPH : IInProcessHost
{
    private HostAdapter IPHHostAdapter;
    public void SetAdapter(IVstaHostAdapter hostAdapter)
    {
        this.IPHHostAdapter = (HostAdapter)hostAdapter;
    }
}

The SetAdapter method receives the HostAdapter from the project client and enables you to customize the project system by accessing properties, methods, and events of the HostAdapter. For example, you can access the DTE property of the HostAdapter to add a menu item to the IDE.

Adding the In-Process Host Assembly to the Global Assembly Cache

You must deploy the in-process host assembly to the global assembly cache so that Visual Studio can load the in-process host into Visual Studio's default application domain. The in-process host must run in Visual Studio's default application domain to interact with the Visual Studio object model. Because assemblies deployed to the global assembly cache must have a strong name, you must sign the in-process host assembly with a strong name before adding it to the global assembly cache.

You can use the Gacutil.exe tool, provided by the Windows Software Development Kit (SDK), to add assemblies to the global assembly cache.

Registering the In-Process Host

Visual Studio Tools for Applications requires that the in-process host be registered, which helps prevent untrusted code from running when a project template is opened. Registering the in-process host is an added security measure because only administrators can install a new in-process host. You must create a GUID for the assembly to be used as the registry key for the in-process host.

Warning

Visual Studio Tools for Applications could load a malicious in-process host that has the same name as the registered in-process host. To prevent an unauthorized in-process host from loading, add a public key token to the Assembly key value for the GUID of each in-process host that you register. In addition, you should load in-process host assemblies from locations that are accessible only to trusted individuals. For more information about adding a public key token to the Assembly key value, see Walkthrough: Creating and Updating an In-Process Host.

To register an in-process host, add the following key to the registry:

  • On 32-bit platforms:

    HKEY_LOCAL_MACHINE\Software\Microsoft\VSTAHostConfig\hostID\2.0\VSTA\InProcHost\GUID

  • On 64-bit platforms:

    HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VSTAHostConfig\hostID\VSTA\InProcHost\GUID

The hostID key identifies your host application. The GUID key identifies the in-process host. You must generate a GUID for the GUID key.

After you add this entry, delete the 9.0 key under the ShapeAppCSharp key, and then run vsta.exe using the /hostid and /setup switch. For more information, see How to: Update the Registration for the Host Application. The in-process host is loaded in both the Visual Studio Tools for Applications IDE and the Visual Studio 2008 IDE.

Adding the In-Process Host to a Project Template

You must add information to the project template so that the in-process host can be identified and loaded. You associate the in-process host with the template project by adding an <InProcHost> node as a child node to the <ProjectClient> node of the project file (ProjectTemplate.csprojor ProjectTemplate.vbproj). The <InProcHost> node should contain the GUID of the in-process host, as shown in the following example:

<InProcHost>{FFD26976-C0AA-4efb-AFF5-63799E13AD98}</InProcHost>

Opening the Project Template

Whenever the add-in developer opens a project, Visual Studio Tools for Applications reads the project file to see if an in-process host node exists. If the in-process host node exists, Visual Studio Tools for Applications locates the GUID in the registry and loads the corresponding in-process host.

After the in-process host is loaded, the HostAdapter creates an instance of the in-process host, and calls the SetAdapter method. The HostAdapter passes itself by reference to the SetAdapter method of the in-process host instance. The in-process host uses the HostAdapter to access the methods, properties, and events of the IDE.

See Also

Tasks

Walkthrough: Creating and Updating an In-Process Host