Share via


Creating and Attaching to Another Instance of Visual Studio .NET

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

You can, for example,

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

Creating Instances of the IDE

To create a new instance of the Visual Studio .NET IDE, use the CreateObject function. You can do either of the following:

  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.CreateObject _
      ("VisualStudio.DTE.7.1")
    
  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.CreateObject _
    
    ("VisualStudio.Solution.7.1")

Both statements create a new instance of the Visual Studio .NET IDE; the first statement directly creates a new instance while the second statement creates a new instance by creating a new solution.

Referencing Instances of the IDE

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

  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.GetObject(, _
      "VisualStudio.DTE.7.1")
    
  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.GetObject(, _
      "VisualStudio.Solution.7.1")
    
  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.GetObject _
      ("C:\Projects\WindowsApplication1\WindowsApplication1.sln")
    
  • Dim inst As Object

    inst = Microsoft.VisualBasic.Interaction.GetObject _
      ("C:\Projects\WindowsApplication1\WindowsApplication1.sln", _
      "VisualStudio.Solution.7.1")
    

See Also

Add-In Registration | Creating Add-Ins and Wizards