Visual Studio 2010 AddIn DTE2 不工作

发布于 2024-12-27 04:15:29 字数 1460 浏览 0 评论 0原文

我正在尝试在 Visual Studio 2010 中创建一个 AddIn,如下所示:

public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    EnvDTE80.Windows2 wins2obj;
    AddIn addinobj;
    object ctlobj = null;
    Window newWinobj;

    // A toolwindow must be connected to an add-in, so this line 
    // references one.
    addinobj = _applicationObject.AddIns.Item(1);
    wins2obj = (Windows2)_applicationObject.Windows;

    // This section specifies the path and class name of the windows 
    // control that you want to host in the new tool window, as well as 
    // its caption and a unique GUID.
    string assemblypath = "C:\\temp\\WindowsControlLibrary1.dll";
    string classname = "WindowsControlLibrary1.UserControl1";
    string guidpos = "{426E8D27-3D33-4FC8-B3E9-9883AADC679F}";
    string caption = "CreateToolWindow2 Test";

    // Create the new tool window and insert the user control in it.
    newWinobj = wins2obj.CreateToolWindow2(addinobj, assemblypath, 
      classname, caption, guidpos, ref ctlobj);
    newWinobj.Visible = true;
}

现在我需要将 DTE2 传递给窗口内新创建的对象 (ctlobj)。如果我在 ctlobj 中声明一个公共变量并在此处设置它,Visual studio 会崩溃并且出现以下异常:

COM Exception was unhandled
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

。有什么想法吗???

谢谢!!

I am trying to create an AddIn in Visual Studio 2010 like below:

public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    EnvDTE80.Windows2 wins2obj;
    AddIn addinobj;
    object ctlobj = null;
    Window newWinobj;

    // A toolwindow must be connected to an add-in, so this line 
    // references one.
    addinobj = _applicationObject.AddIns.Item(1);
    wins2obj = (Windows2)_applicationObject.Windows;

    // This section specifies the path and class name of the windows 
    // control that you want to host in the new tool window, as well as 
    // its caption and a unique GUID.
    string assemblypath = "C:\\temp\\WindowsControlLibrary1.dll";
    string classname = "WindowsControlLibrary1.UserControl1";
    string guidpos = "{426E8D27-3D33-4FC8-B3E9-9883AADC679F}";
    string caption = "CreateToolWindow2 Test";

    // Create the new tool window and insert the user control in it.
    newWinobj = wins2obj.CreateToolWindow2(addinobj, assemblypath, 
      classname, caption, guidpos, ref ctlobj);
    newWinobj.Visible = true;
}

Now I need to pass DTE2 to the newly created object inside the window (ctlobj). If I declare a public variable in ctlobj and set it here, Visual studio crashes and I get this exception:

COM Exception was unhandled
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

. Any ideas ???

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2025-01-03 04:15:29

不过有一个解决方法:

// 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");

There a workaround though:

// 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");
oО清风挽发oО 2025-01-03 04:15:29

我知道你从纳扎夫那里得到了答案,还有另一种方法,它更干净一点。将 dte 对象设置为外接程序 class 中的公共属性,并将外接程序类传递给 newWinObj

然后您的 newWinObj 将可以访问 addin 和 dte 对象。可以用,我用的就是这个系统。

I know you have the answer from Nazaf, there is another way, it is a little bit cleaner. Make the dte object a public property in your addin class and pass the addin class to the newWinObj.

Then your newWinObj will have access to both the addin and the dte objects. It works, I use this system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文