Export (0) Print
Expand All

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

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see How to: Convert an Add-in to a VSPackage.

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 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 Export Settings on the Tools menu. For more information, see Customizing Development Settings.

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

' CreateObject method 1 - VisualStudio.DTE.11.0.
Public Sub Exec(ByVal commandName As String, ByVal executeOption _
As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As _
Object,  ByRef handled As Boolean) Implements IDTCommandTarget.Exec
   handled = False
   If executeOption = _
      vsCommandExecOption.vsCommandExecOptionDoDefault Then
      If commandName = "VBTestAddin.Connect.VBTestAddin" Then
          CreateNewInstance1(_applicationObject)
          handled = True
          Exit Sub
      End If
   End If
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.11.0", "")
    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;
    CreateNewInstance2(_applicationObject);
}

private void CreateNewInstance2(DTE2 dte)
{
    System.Type Type;
    object inst;
    EnvDTE80.DTE2 dte80Obj;
    Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.11.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.11.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. 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.11.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")
Show:
© 2014 Microsoft