如何让DTE运行Visual Studio实例?

如何获取Visual Studio的所有运行实例以便我可以自动执行?

(添加了这个问题,因为这个问题已经关闭)

使用运行对象表获取所有实例,然后选择所需的实例。

我认为你不能比这更好。 它类似于将调试器附加到VS实例的方式。 您必须从列表中选择一个。

IEnumerable GetInstances() { IRunningObjectTable rot; IEnumMoniker enumMoniker; int retVal = GetRunningObjectTable(0, out rot); if (retVal == 0) { rot.EnumRunning(out enumMoniker); IntPtr fetched = IntPtr.Zero; IMoniker[] moniker = new IMoniker[1]; while (enumMoniker.Next(1, moniker, fetched) == 0) { IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); string displayName; moniker[0].GetDisplayName(bindCtx, null, out displayName); Console.WriteLine("Display Name: {0}", displayName); bool isVisualStudio = displayName.StartsWith("!VisualStudio"); if (isVisualStudio) { object obj; rot.GetObject(moniker[0], out obj); var dte = obj as DTE; yield return dte; } } } } [DllImport("ole32.dll")] private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc); [DllImport("ole32.dll")] private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot); 

以下是VB.Net获取CURRENT Visual Studio实例的解决方案。

我所做的是与使用当前程序集名称调试的进程进行简单的字符串比较。 当VS的多个实例打开时,它似乎按预期工作,我在Visual Studio 2013中以调试和发布模式尝试它。

import:

 Imports System Imports System.Diagnostics.CodeAnalysis Imports System.Runtime.InteropServices Imports System.Runtime.InteropServices.ComTypes Imports EnvDTE80 

P /调用:

(NativeMethods.dll)

 ''' ---------------------------------------------------------------------------------------------------- '''  ''' Returns a pointer to an implementation of  (a bind context object). '''  ''' This object stores information about a particular moniker-binding operation. '''  ''' ---------------------------------------------------------------------------------------------------- '''  '''  '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' This parameter is reserved and must be 0. '''  ''' '''  ''' Address of an  pointer variable that receives the ''' interface pointer to the new bind context object. '''  ''' When the function is successful, the caller is responsible for calling Release on the bind context. '''  ''' A value of  for the  value indicates that an error occurred. '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' This function can return the standard return values E_OUTOFMEMORY and S_OK. '''  ''' ----------------------------------------------------------------------------------------------------   Public Shared Function CreateBindCtx(ByVal reserved As Integer, ByRef ppbc As IBindCtx ) As Integer End Function ''' ---------------------------------------------------------------------------------------------------- '''  ''' Returns a pointer to the  interface on the local running object table (ROT). '''  ''' ---------------------------------------------------------------------------------------------------- '''  '''  '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' This parameter is reserved and must be 0. '''  ''' '''  ''' The address of an  pointer variable that receives the ''' interface pointer to the local ROT. '''  ''' When the function is successful, the caller is responsible for calling Release on the interface pointer. '''  ''' A value of  for the  value indicates that an error occurred. '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' This function can return the standard return values E_UNEXPECTED and S_OK. '''  ''' ----------------------------------------------------------------------------------------------------   Public Shared Function GetRunningObjectTable(ByVal reserved As Integer, ByRef pprot As IRunningObjectTable ) As Integer End Function 

其余代码:

 ''' ---------------------------------------------------------------------------------------------------- '''  ''' Gets a collection of the Visual Studio instances that are running on this PC. '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' An  that contains the running Visual Studio instances, if any. '''  ''' ---------------------------------------------------------------------------------------------------- Public Shared Iterator Function GetVisualStudioInstances() As IEnumerable(Of DTE2) Dim rot As IRunningObjectTable = Nothing Dim enumMoniker As IEnumMoniker = Nothing Dim retVal As Integer = NativeMethods.GetRunningObjectTable(0, rot) If (retVal = 0) Then rot.EnumRunning(enumMoniker) Dim fetched As IntPtr = IntPtr.Zero Dim moniker As IMoniker() = New IMoniker(0) {} While (enumMoniker.Next(1, moniker, fetched) = 0) Dim bindCtx As IBindCtx = Nothing NativeMethods.CreateBindCtx(0, bindCtx) Dim displayName As String = "" moniker(0).GetDisplayName(bindCtx, Nothing, displayName) If (displayName.StartsWith("!VisualStudio")) Then Dim obj As New Object rot.GetObject(moniker(0), obj) Yield DirectCast(obj, DTE2) End If End While End If End Function ''' ---------------------------------------------------------------------------------------------------- '''  ''' Gets a  object that represents the current Visual Studio instance that is running this project. '''  ''' ---------------------------------------------------------------------------------------------------- '''  ''' A  object that represents the current Visual Studio instance that is running this project. '''  ''' ---------------------------------------------------------------------------------------------------- Public Shared Function GetCurrentVisualStudioInstance() As DTE2 Dim currentInstance As DTE2 = Nothing Dim processName As String = Process.GetCurrentProcess.MainModule.FileName Dim instances As IEnumerable(Of DTE2) = GetVisualStudioInstances For Each instance As DTE2 In instances For Each p As EnvDTE.Process In instance.Debugger.DebuggedProcesses If (p.Name = processName) Then currentInstance = instance Exit For End If Next p Next instance Return currentInstance End Function