I want to get a reference to the current solution, using the DTE2 object with C# in Visual Studio 2010.
I first tried the following code:
var dte = M
for anyone interested in doing this with F# a mostly complete conversion is here ( currently set to run in linqpad):
open System;
open System.Runtime.InteropServices;
open System.Runtime.InteropServices.ComTypes;
open EnvDTE;
open System.Diagnostics;
//http://stackoverflow.com/questions/10864595/getting-the-current-envdte-or-iserviceprovider-when-not-coding-an-addin
//http://stackoverflow.com/questions/6558789/how-to-convert-out-ref-extern-parameters-to-f
//http://stackoverflow.com/questions/1689460/f-syntax-for-p-invoke-signature-using-marshalas
[]
extern int CreateBindCtx(System.IntPtr inRef, IBindCtx& outParentRef);
[]
extern int GetRunningObjectTable(System.IntPtr inRef, IRunningObjectTable& outParentRef);
//let dte = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0") :?> EnvDTE80.DTE2
let comName="VisualStudio.DTE.12.0"
let rotEntry = "!"+comName
//let mutable rot:IRunningObjectTable =null
let rot=
let mutable result:IRunningObjectTable = null
GetRunningObjectTable(nativeint 0, &result) |> ignore
result
let mutable enumMoniker:IEnumMoniker = null
rot.EnumRunning (&enumMoniker)
enumMoniker.Reset() |> ignore
let mutable fetched = IntPtr.Zero
let mutable moniker:IMoniker[] = Array.zeroCreate 1 //http://msdn.microsoft.com/en-us/library/dd233214.aspx
let matches = seq {
while enumMoniker.Next(1, moniker, fetched) = 0 do
"looping" |> Dump
let mutable bindCtx:IBindCtx = null
CreateBindCtx(nativeint 0, &bindCtx) |> ignore
let mutable displayName:string = null
moniker.[0].GetDisplayName(bindCtx,null, &displayName)
displayName |> Dump
if displayName.StartsWith(rotEntry) then
let mutable comObject = null
rot.GetObject(moniker.[0], &comObject) |> ignore
let dte = comObject:?>EnvDTE80.DTE2
yield displayName,bindCtx,comObject,dte.FullName, dte
}
matches |> Dump