Current Entries | Archives   RSS

 


Visual Studio .NET Add-in installation

Tuesday 8th March, 2005


I said in my previous post I'd talk about add-in installation.

One of the most interesting ones concerns the installation of the add-in. Unlike the skeleton add-ins created by the Visual Studio .NET Add-in Wizard, Visual Lint creates its registry keys on registration as described by Oz Solomon in his article Project Line Counter Add-In v2.10 for VS.NET and VC6. Rather than duplicate that information here, I suggest you go and read the article - it's really quite interesting. :)

More interestingly, Visual Lint also removes its commands when unregistered (which happens using the uninstallation process). Although managed add-ins can do this easily by including a class derived from System.Configuration.Install.Installer, for unmanaged add-ins it's not so obvious how to implement the same functionality.

As you can see in the code sample below, we chose to remove commands in DllUnregisterServer(). The important bit is that in order to do so, an instance of the corresponding implementation of DTE needs to be created. As it turns out, this is really, really simple:

    // Create an instance of VS.NET 2003
    EnvDTE::_DTEPtr ptrDTE = NULL;
    ptrDTE.CreateInstance(L"VisualStudio.DTE.7.1");

Once an instance of DTE is available, it can be passed to methods in the add-in which use it to remove the add-in's commands from the corresponding version of Visual Studio, as the following code from Visual Lint shows:

static HRESULT RemoveCommands(LPCWSTR pszVisualStudioProgID)
{
    HRESULT hr = E_FAIL;

    try
    {
        EnvDTE::_DTEPtr ptrDTE = NULL;
        ptrDTE.CreateInstance(pszVisualStudioProgID);
        if (ptrDTE != NULL)
        {
            CVsCommandsManager commands;

            commands.OnConnection(ptrDTE.GetInterfacePtr(), NULL, NULL, NULL);
            commands.RemoveCommands();

            hr = S_OK;

            ptrDTE->Quit();

            ptrDTE.Release();
        }
    }
    catch (_com_error& e)
    {
        hr = e.Error();
    }
    return hr;
}

STDAPI DllUnregisterServer(void)
{
    // Remove commands from VS2005, 2003 and 2002 respectively
    HRESULT hrVS2005 = RemoveCommands( L"VisualStudio.DTE.8.0");
    HRESULT hrVS2003 = RemoveCommands( L"VisualStudio.DTE.7.1");
    HRESULT hrVS2002 = RemoveCommands( L"VisualStudio.DTE.7");

    HRESULT hr = _Module.DllUnregisterServer();

    return hr;
}

Note that CVsCommandsManager is part of our implementation and not a class generated by the add-in wizard. Whilst its normal job is to manage the CVsCommandHandler objects which implement each of Visual Lint's commands, it is also created in a standalone form during add-in uninstallation as seen above.

Together, these changes seem to have the potential to greatly reduce installation issues, and we fully intend to include this functionality in the next version of ResOrg.

Posted by Anna at 7:28pm | Get Permalink