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:
Create a new instance of Visual Studio where the instance remains loaded even when:
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 Export Settings on the Tools menu. For more information, see Visual Studio Settings.
Example
To create a new instance of Visual Studio, use System.Activator.CreateInstance. The example below illustrates this method.
' GetTypeFromProgID and CreateInstance method.
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
CreateNewInstance2(_applicationObject)
handled = True
Exit Sub
End If
End If
End Sub
Private Sub CreateNewInstance2(ByVal dte As DTE2)
Dim Type As System.Type
Dim inst As Object
Dim dte80Obj As EnvDTE80.DTE2
Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.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;
CreateNewInstance2(_applicationObject);
}
private void CreateNewInstance2(DTE2 dte)
{
System.Type Type;
object inst;
EnvDTE80.DTE2 dte80Obj;
Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.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.10.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.