static _DTE GetCurrentDTE() { EnvDTE.DTE dte = null; // i take ROT UCOMIRunningObjectTable rot; uint uret = GetRunningObjectTable(0, out rot); if (uret == S_OK) { // I Take emunerator UCOMIEnumMoniker EnumMon; rot.EnumRunning(out EnumMon); if (EnumMon != null) { // object limit const int NUMMONS = 5000; UCOMIMoniker[] aMons = new UCOMIMoniker[NUMMONS]; int Fetched = 0; EnumMon.Next(NUMMONS, aMons, out Fetched); // i'm creating binding to monikers. string Name; UCOMIBindCtx ctx; uret = CreateBindCtx(0, out ctx); // ROT name of _DTE using id process System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); string dteName = "VisualStudio.DTE"; for (int i = 0; i < Fetched; ++i) { // i take the name aMons[i].GetDisplayName(ctx, null, out Name); // found? if (Name.IndexOf(dteName) != -1) { object temp; rot.GetObject(aMons[i], out temp); dte = (EnvDTE.DTE)temp; break; } } } } return(dte); }
internal static DTE GetCurrentDTEObject() { EnvDTE.DTE dte = null; // Get the ROT UCOMIRunningObjectTable rot; int uret = GetRunningObjectTable(0, out rot); if (uret == S_OK) { // Get an enumerator to access the registered objects UCOMIEnumMoniker EnumMon; rot.EnumRunning(out EnumMon); int fetched = 0; UCOMIMoniker [] aMons = new UCOMIMoniker[1]; if (EnumMon != null) { string dteNameStart = "VisualStudio.DTE."; string dteNameEnd = ":" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); while (EnumMon.Next(1, aMons, out fetched) == 0) { // Set up a bindind context so that we can access the monikers string name; UCOMIBindCtx ctx; uret = CreateBindCtx(0, out ctx); // Get the display string aMons[0].GetDisplayName(ctx, null, out name); // if this is the one we are interested in.. if (name.IndexOf(dteNameStart) != -1 && name.EndsWith(dteNameEnd)) { object temp; rot.GetObject(aMons[0], out temp); dte = (EnvDTE.DTE) temp; break; } } } } return dte; }
/// <summary> /// Returns a reference to the Development Tools Extensibility (DTE) object /// </summary> /// <returns>DTE object</returns> public static EnvDTE._DTE GetDTE() { if (DTE != null) { return(DTE); } // Get the DTE // The following method works fine IF you only have one instance of VS .NET open... // It returns the first created instance of VS .NET...not necessarily the current instance! // return (EnvDTE._DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE"); // This code was adapted from Girish Hegde's on-line article // "Adding or Changing Code using FileCodeModel in Visual Studio .NET EnvDTE.DTE dte = null; // Get the ROT UCOMIRunningObjectTable rot; uint uret = GetRunningObjectTable(0, out rot); if (uret == S_OK) { // Get an enumerator to access the registered objects UCOMIEnumMoniker EnumMon; rot.EnumRunning(out EnumMon); if (EnumMon != null) { // Just grab a bunch of them at once, 100 should be // plenty for a test const int NumMons = 100; UCOMIMoniker[] aMons = new UCOMIMoniker[NumMons]; int Fetched = 0; EnumMon.Next(NumMons, aMons, out Fetched); // Set up a binding context so we can access the monikers string Name; UCOMIBindCtx ctx; uret = CreateBindCtx(0, out ctx); // Create the ROT name of the _DTE object using the process id System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); string dteName = "VisualStudio.DTE."; string processID = ":" + currentProcess.Id.ToString(); // for each moniker retrieved for (int i = 0; i < Fetched; i++) { // Get the display string aMons[i].GetDisplayName(ctx, null, out Name); // If this is the one we are interested in... if (Name.IndexOf(dteName) != -1 && Name.IndexOf(processID) != -1) { object temp; rot.GetObject(aMons[i], out temp); dte = (EnvDTE.DTE)temp; break; } } } } // *** Cache the reference if found DTE = dte; return(dte); }