How to: Create and Attach to Another Instance of Visual Studio

In some cases it is useful to programmatically create a new instance of Visual Studio or attach to a specific instance of Visual Studio that is already running. If two instances of Visual Studio are running on a system and both have the same solution open — for example, one instance is performing a solution build and the other is perform a debug build — you can program your add-in to differentiate between them.

You can, for example:

  • Start an instance of Visual Studio based on a path to a file or solution.

  • Attach to an instance of Visual Studio based on a path to a file or solution.

  • Load a file or solution into an existing instance of Visual Studio.

  • Create a new instance of Visual Studio where the instance shuts down when:

    • The external reference count on both the DTE object and the Solution object are 0.

      -and-

    • The integrated development environment (IDE) is not displayed to the user or is not under user control.

  • Create a new instance of Visual Studio where the instance remains loaded even when:

    • The external reference count on both the DTE object and the Solution object are 0.

    • The IDE is displayed to the user.

      -and-

    • The IDE is under user control.

The returned objects can be cast to their respective objects, such as DTE2 and Solution2.

Note

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

Example

To create a new instance of Visual Studio, use either the CreateObject Function (Visual Basic) or System.Activator.CreateInstance. When using the CreateObject function in Visual Basic, you can pass values of either "VisualStudio.DTE.8.0" or "VisualStudio.Solution.8.0." The examples below illustrate these three methods.

' CreateObject method 1 - VisualStudio.DTE.8.0.
Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    CreateNewInstance1(_applicationObject)
End Sub

Private Sub CreateNewInstance1(ByVal dte As DTE2)
    Dim inst As Object
    Dim dte80Obj As EnvDTE80.DTE2
    inst = Microsoft.VisualBasic.Interaction. _
    CreateObject("VisualStudio.DTE.8.0", "")
    dte80Obj = CType(inst, EnvDTE80.DTE2)
    MsgBox(dte80Obj.DisplayMode.ToString)
End Sub
' CreateObject method 2 - VisualStudio.Solution.8.0.
Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    CreateNewInstance2(_applicationObject)
End Sub

Private Sub CreateNewInstance2(ByVal dte As DTE2)
    Dim inst As Object
    Dim dte80Obj As EnvDTE80.DTE2
    inst = Microsoft.VisualBasic.Interaction. _
    CreateObject("VisualStudio.Solution.8.0", "")
    dte80Obj = CType(inst, EnvDTE80.DTE2)
    MsgBox(dte80Obj.DisplayMode.ToString)
End Sub
' GetTypeFromProgID and CreateInstance method.
Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    CreateNewInstance3(_applicationObject)
End Sub

Private Sub CreateNewInstance3(ByVal dte As DTE2)
    Dim Type As System.Type
    Dim inst As Object
    Dim dte80Obj As EnvDTE80.DTE2
    Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0")
    inst = System.Activator.CreateInstance(Type, True)
    dte80Obj = CType(inst, EnvDTE80.DTE2)
    MsgBox(dte80Obj.DisplayMode.ToString)
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    CreateNewInstance3(_applicationObject);
}

private void CreateNewInstance3(DTE2 dte)
{
    System.Type Type;
    object inst;
    EnvDTE80.DTE2 dte80Obj;
    Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
    inst = System.Activator.CreateInstance(Type, true);
    dte80Obj = (EnvDTE80.DTE2)inst;
    System.Windows.Forms.MessageBox.Show
      (dte80Obj.DisplayMode.ToString());
}
// Create a new instance of Visual Studio by using 
// GetTypeFromProgID and CreateInstance.
private void CreateNewInstance1()
{
    System.Type type = System.Type.GetTypeFromProgID
    ("VisualStudio.DTE.8.0");
    Object obj = System.Activator.CreateInstance(type, true);
    EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)obj;
}

In the Visual Basic example, both statements create a new instance of the Visual Studio IDE. The first statement directly creates a new instance while the second statement creates a new instance by creating a new solution.

To obtain a reference to an existing instance of the Visual Studio IDE, you can use the GetObject Function (Visual Basic). You can do one of the following:

Private Sub GetInstance1 ()
    Dim inst As Object
    Dim dte80Obj As EnvDTE80.DTE2
    inst = Microsoft.VisualBasic.Interaction.GetObject(, _
    "VisualStudio.DTE.8.0")
    dte80Obj = CType(inst, EnvDTE80.DTE2)
    MsgBox(dte80Obj.DisplayMode.ToString)
End Sub

' -or-
Dim inst As Object
inst = Microsoft.VisualBasic.Interaction.GetObject(, _
  "VisualStudio.Solution.8.0")
' ---------------------------
' -or-
' Change the path to your application.
Dim inst As Object
inst = Microsoft.VisualBasic.Interaction.GetObject _
  ("C:\Projects\WindowsApplication1\WindowsApplication1.sln")
' ---------------------------
' -or-
' Change the path to your application.
Dim inst As Object
inst = Microsoft.VisualBasic.Interaction.GetObject _
  ("C:\Projects\WindowsApplication1\WindowsApplication1.sln", _
  "VisualStudio.Solution.8.0")

See Also

Concepts

Add-in Registration

Other Resources

Creating Add-ins and Wizards