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!!
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");
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.
User contributions licensed under CC BY-SA 3.0