locked
Documents collection contains documents from another instance of Visual Studio RRS feed

  • Question

  • Within a visual studio package I have code that uses InteropServices.Marshal.GetActiveObject to get a reference to the DTE2 object. From there I list the names of the documents in the Documents collection. Problem is, when I test the package, the documents listed are the documents from the instance of Visual Studio that launches the experimental instance that the package is running in.   How to list the documents of the experimental instance?

          // Get an instance of the currently running Visual Studio IDE.
          EnvDTE80.DTE2 dte2;
          dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal
            .GetActiveObject("VisualStudio.DTE.10.0");
          if (dte2 == null)
            Trace.WriteLine("did not get dte reference");
          else
          {
    
            foreach (var item in dte2.Documents)
            {
              var doc = item as EnvDTE.Document;
              if (doc != null)
              {
                Trace.WriteLine("document:" + doc.Name + " " + doc.Path);
              }
            }
    
            var s1 = dte2.ActiveDocument.FullName;
            Trace.WriteLine("active document:" + s1);
          }
    

    Thursday, March 22, 2012 3:10 PM

Answers

  • GetActiveObject uses the ROT. Every instance of Visual Studio registers itself in the ROT. If you have a package why are you using GetActiveObject? Just use GetService on the package class, or ServiceProvider.GlobalProvider to get the DTE instance.

    Ryan

    • Marked as answer by Steve Richter Thursday, March 22, 2012 4:27 PM
    Thursday, March 22, 2012 3:30 PM