Get the reference of the DTE2 object in Visual C# 2010

后端 未结 5 1729
北海茫月
北海茫月 2020-12-09 05:32

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         


        
5条回答
  •  不知归路
    2020-12-09 06:02

    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
    

提交回复
热议问题