DTE.Solution bad value

  • Thread starter Thread starter Sebastien Lange
  • Start date Start date
S

Sebastien Lange

Hi,

While designing a windows form, one of my controls needs to know the
solution path.
So I'm getting an instance of DTE (see hereafter) and call the Solution
property, then its FileName.
It's working fine until you have several VS.NET opened, then it returns the
path of the solution of the first openened VS.NET, not the current one
anymore...

Is it a bug, is there a workaround? (I'm using VS.NET 2003 Pro)

Sebastien

EnvDTE.DTE dte =
(EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualSt
udio.DTE.7.1"); // Get an instance of the currently running Visual Studio
..NET IDE.

MessageBox.Show(dte.Solution.FileName);
 
Hi Sebastien,

GetActiveObject could not gurantee to return the current object, since it
might not be registed as active object of this COM class.

However, we may try enumerating the COM Running Object Table to find the
reference to the current object. Here is the sample snippet:
<code>
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using EnvDTE;
using System.Runtime.InteropServices;

namespace DTE_TEst
{
[
Designer(typeof(MyCtlDesigner))
]
public class MyCtl : System.Windows.Forms.TextBox
{
private System.ComponentModel.Container components = null;
public MyCtl()
{
InitializeComponent();
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}

class MyCtlDesigner : ControlDesigner
{
public override void OnSetComponentDefaults()
{
//Moniker string definition: "!VisualStudio.DTE.7.1:<pid>"
string strMoniker = "!VisualStudio.DTE.7.1:" +
System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
MessageBox.Show(strMoniker);

EnvDTE._DTE dte =(EnvDTE._DTE)GetMSDEVFromGIT(strMoniker);
this.Control.Text = dte.Solution.FullName;
}

[DllImport("ole32.dll")]
public static extern int GetRunningObjectTable(int reserved, out
UCOMIRunningObjectTable prot);

[DllImport("ole32.dll")]
public static extern int CreateBindCtx(int reserved, out UCOMIBindCtx
ppbc);


public object GetMSDEVFromGIT(string strProgID)
{
UCOMIRunningObjectTable prot;
UCOMIEnumMoniker pMonkEnum;

GetRunningObjectTable(0,out prot);
prot.EnumRunning(out pMonkEnum);
pMonkEnum.Reset();
int fetched;
UCOMIMoniker []pmon = new UCOMIMoniker[1];
while(pMonkEnum.Next(1, pmon, out fetched) == 0)
{
UCOMIBindCtx pCtx;
CreateBindCtx(0, out pCtx);
string str;
pmon[0].GetDisplayName(pCtx,null,out str);
if(str == strProgID)
{
object objReturnObject;
prot.GetObject(pmon[0],out objReturnObject);
object ide = (object)objReturnObject;
return ide;
}
}
return null;
}
}
}
</code>

Does it solve your problem?
Please feel free to reply this thread if you still have problem on this
issue.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Back
Top