Reference to ToolWindow from within Custom Tool

I've created a ToolWindow in a VsPackage. In the same assembly I also created a CustomTool for generating some code.

How can I get a reference to the ToolWindow I've created via the VsPackage Is this possible



Answer this question

Reference to ToolWindow from within Custom Tool

  • Eddie Hulme

    Hi Stefan,

    Guess I should have tried it before posting

    A single file generator is sited with the project item's IServiceProvider which only recognizes a small number of services:

    • VxDTE::SID_SVSProjectItem
    • SID_SVSWebReferenceDynamicProperties
    • IID_IVsHierarchy
    • SID_SVsApplicationSettings

    The first is the DTE ProjectItem, which will get you access to the DTE object, which in turn implements an IServiceProvider with access to the shell services.

    So if you're using that GeneratorSample's BaseCodeGeneratorWithSite, you can get at the IVsUIShell interface using code similar to the following (provided you invoke this after the IObjectWithSite.SetSite is called.

    using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

    ProjectItem projItem = GetProjectItem();
    ServiceProvider sp = new ServiceProvider((IOleServiceProvider)projItem.DTE);
    IVsUIShell shell = (IVsUIShell)sp.GetService(typeof(SVsUIShell));

    Sincerely,



  • davisp2

    I have tried that. I do get a reference to IVsUIShell, but then the FindToolWindow call throws an exception (null reference).

    This is the code:

    Guid myWindow = new Guid("35752e3d-8335-4214-84b5-365479bf375d");
    IVsWindowFrame frame = null;
    int result = shell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFrameOnly, ref myWindow, out frame);

    The Guid mentioned above is the Guid that my Package has assigned to the toolwindow.

    Am I doing something wrong here


  • Tom Arnold

    Investigating further... problem is that that the shell object stays null.

    With the sample provided in the dec drop I recreated my custom tool and tried to get the service:

    IVsUIShell shell = (IVsUIShell)GetService(typeof(SVsUIShell));

    But shell always stays null, and of course further use (as above) is useless.

    So how to get the shell right


  • blackspider

    Hi Stefan,

    If your custom tool implements IOleObjectWithSite, you should get an IServiceProvider from which you can retrieve the IDE's IVsUIShell interface, and then invoke the FindToolWindow method.

    There is a sample in the December CTP drop of the Visual Studio SDK, called "GeneratorSample" that illustrates a single file generator (aka custom tool) that also implements IOleObjectWithSite. You should be able to just add and derive your single file generator object from the BaseCodeGeneratorWithSite, and use it's GetService method to retrieve the IVsUIShell interface.

    Sincerely,



  • Reference to ToolWindow from within Custom Tool