Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Visual Studio Tool Window - VsAddin or VsPackage

Simple question - I found two ways to add a tool window to Visual Studio (2008): create an addin or create a package.

(Addin: http://www.codeproject.com/KB/dotnet/vstoolwindow.aspx)
(Package: http://msdn.microsoft.com/en-us/library/bb165051.aspx)

What's the "right" way?

like image 235
Arielr Avatar asked Dec 17 '22 06:12

Arielr


2 Answers

You can do either, and I've done both. In some ways, addins are a bit easier, but they suffer from some annoying drawbacks.

Add-in:

  • +No requirement to install the Visual Studio SDK
  • +No requirement to use a Package Load Key (PLK) or sign your distributed binaries
  • +Easier development process when you use the Extensibility API (simplified code in many ways)
  • +Easier installation process (no wacky registry stuff)
  • -Doesn't seem to behave as well as VSPackage-based tool panes
  • -I've run into performance issues which prompted me to use the VS SDK COM interfaces instead of the Extensibility ones, leading to a significant performance bump. In other words, my "add-in" is now based on the VS SDK and is only really an add-in because it loads via an XML file instead of the registry. (Also, from everything I can tell, the Extensibility interfaces are just a large utility wrapper around the SDK.)

VSPackages:

  • +You can harness the full power of the VS SDK, both its feature set and (potentially, when carefully used) performance advantage
  • +Seems to behave more reliably than add-ins
  • -Requires signed binaries, a PLK, and a complicated installation procedure
  • -Steep learning curve, and many seemingly simple actions are nasty/convoluted. I now have an assembly providing extension methods to perform "obvious (to me)" actions on the COM interfaces. Between that and experience things have improved over time. There are similar options available to the community, which you should seriously consider if you go this route.
like image 110
Sam Harwell Avatar answered Jan 17 '23 16:01

Sam Harwell


I think 280Z28 was perfectly correct prior VS2010. But now VS2010 and VS012:

  • Do not require officially signed packages (may only those that go to the Gallery have to);
  • Thanks to VSIX it can very easily install VSPackages that can also be deployed to the Gallery.

Moreover VS2010 supports another kind of extensibility: those are MEF extensions, that are lightweight plugins that trigger only at specific events of the IDE, like text editor events. An example is FixMixedTabs extension.

Just a create a VSPackage empty package (no menus, commands, ...) and copy this in the main class to create a VSPackage that basically loads when there's an active solution and just get a reference to the DTE2. In this way you can just use it as an Add-in.

// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
// a package.
[PackageRegistration(UseManagedResourcesOnly = true)]
// This attribute is used to register the informations needed to show the this package
// in the Help/About dialog of Visual Studio.
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
// Load this package when a solution is loaded (VSConstants.UICONTEXT_SolutionExists)
[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
public sealed class VSPackage1Package : Package
{
    /// <summary>
    /// Default constructor of the package.
    /// Inside this method you can place any initialization code that does not require 
    /// any Visual Studio service because at this point the package object is created but 
    /// not sited yet inside Visual Studio environment. The place to do all the other 
    /// initialization is the Initialize method.
    /// </summary>
    public VSPackage1Package()
    {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
    }

    /// <summary>
    /// Initialization of the package; this method is called right after the package is sited, so this is the place
    /// where you can put all the initilaization code that rely on services provided by VisualStudio.
    /// </summary>
    protected override void Initialize()
    {
        Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
        base.Initialize();

        IVsExtensibility extensibility =
            GetService(typeof(EnvDTE.IVsExtensibility)) as
            IVsExtensibility;
        DTE2 dte = extensibility.GetGlobalsObject(null).DTE as DTE2;
    }
}
like image 45
ceztko Avatar answered Jan 17 '23 15:01

ceztko