Current Entries | Archives   RSS

 


Why on earth can't they just leave interfaces alone?

Tuesday 17th January, 2006


...or: "why is it so difficult to figure out how to read the Visual C++ Include Folder configuration in VS2005?"

The answer is of course that the interfaces have been changed. I don't know why, but I do know that the method recommended by Microsoft no longer works.

In VS2002 and VS2003 expose interfaces to a collection of VCPlatform objects via DTE's properties collection, indexed by the name of the platform. Once you have a VCPlatform interface pointer, reading the include folder configuration is simplicity itself:

CString GetVcIncludeFolders(EnvDTE::_DTE* pDTE, const CString& sPlatform /*= _T("Win32")*/ )
{
    CString sIncludeFolders;

    try
    {
        EnvDTE::_DTEPtr ptrDTE;
        pDTE->QueryInterface(&ptrDTE);

        EnvDTE::PropertiesPtr ptrProperties = ptrDTE->GetProperties(L"Projects", L"VCDirectories");

        VCProjectEngineLibrary::IVCCollectionPtr ptrPlatforms = ptrProperties->Item(L"Platforms")->GetObject();
        ATLASSERT(ptrPlatforms != NULL);

        if (ptrPlatforms != NULL)
        {
            VCProjectEngineLibrary::VCPlatformPtr ptrPlatform = ptrPlatforms->Item(_bstr_t(sPlatform) );

            _bstr_t bsFolders = ptrPlatform->GetIncludeDirectories();

            sIncludeFolders = COLE2CT( ptrPlatform->Evaluate(bsFolders) );
        }
    }
    catch (const _com_error& e)
    {
#ifdef _DEBUG
         CString sMsg;
        sMsg.Format(_T("Unexpected exception 0x%X (%s) in %s()\n"),
                    e.Error(),
                     e.ErrorMessage(),
                    CA2CT( __FUNCTION__ ) );

        ATLTRACE(sMsg);
        ATLASSERT(false);
#endif
    }
    return sIncludeFolders;
}

Unfortunately, the available properties have changed in VS2005, and the "Platforms" collection is no longer exposed in this way, as a glance at the properties shown by the Extensibility Browser will show:

VCDirectories properties in VS2003
VCDirectories properties in Visual Studio .NET 2003.
VCDirectories properties in VS2005
VCDirectories properties in Visual Studio 2005.

When we realised this our first thought was to try to obtain a VCProjectEngine interface somehow - from there the Platforms collection is directly accessible. Unfortunately all the examples in MSDN and elsewhere show VCProjectEngine interfaces being obtained from an EnvDTE::Project object, as follows (apologies for the VB!), and we can't use this method if there is no solution open:

' add reference to Microsoft.VisualStudio.VCProjectEngine

Imports EnvDTE
Imports Microsoft.VisualStudio.VCProjectEngine

Public Module Module1
    Sub Test()
        Dim prj As VCProject
        Dim cfgs, tools As IVCCollection
        Dim cfg As VCConfiguration
        Dim p As VCPlatform
        Dim x As String
        prj = DTE.Solution.Projects.Item(1).Object
        p = prj.Platforms(1)
        x = p.IncludeDirectories
        p.IncludeDirectories = x + ";something"
        MsgBox(p.IncludeDirectories)
    End Sub
End Module

The key was finding a way to obtain a VCProjectEngine interface pointer without going through a Project. Although Microsoft suggest you can create an instance of this object directly like this:

    VCProjectEngineLibrary::VCProjectEnginePtr ptrEngine;

    ptrEngine.CreateInstance( __uuidof(VCProjectEngineLibrary::VCProjectEngineObject),
                              NULL,
                              CLSCTX_INPROC_SERVER);

this is not a safe operation to perform within the IDE, certainly under VS2005. We saw some sorts of strange and unpleasant side effects when we explored this route!

Thankfully there is another way, albeit an undocumented one. The VCProjects collection (accessible via a property of DTE itself) can be used to obtain a VCProjectEngine interface pointer, and thereafter it is straightforward:

CString GetVcIncludeFolders(EnvDTE::_DTE* pDTE, const CString& sPlatform /*= _T("Win32")*/)
{
    CString sIncludeFolders;

    if (pDTE != NULL)
    {
        try
        {
            EnvDTE::_DTEPtr ptrDTE;
            pDTE->QueryInterface(&ptrDTE);

            EnvDTE::ProjectsPtr ptrProjects = pDTE->GetObject(L"VCProjects");

            VCProjectEngineLibrary::VCProjectEnginePtr ptrProjectEngine =
                ptrProjects->GetProperties()->Item(L"VCProjectEngine")->GetObject();

            if (ptrProjectEngine != NULL)
            {
                VCProjectEngineLibrary::IVCCollectionPtr ptrVcPlatforms =
                    ptrVCProjectEngine->GetPlatforms();

                VCProjectEngineLibrary::VCPlatformPtr ptrPlatform = ptrVcPlatforms->Item( _bstr_t(sPlatform) );

                sIncludeFolders = COLE2CT( ptrPlatform->Evaluate( ptrPlatform->GetIncludeDirectories() ) );
            }
        }
        catch (const _com_error& e)
        {
#ifdef _DEBUG
            CString sMsg;
            sMsg.Format(_T("Unexpected exception 0x%X (%s) in %s()\n"),
            e.Error(),
            e.ErrorMessage(),
            CA2CT( __FUNCTION__() ) );

            ATLTRACE(sMsg);
            ATLASSERT(false);
#endif
        }
    }
    ATLASSERT(!sIncludeFolders.IsEmpty() );

    return sIncludeFolders;
}

Fortunately, the same code also works in VS2002 and VS2003.

Posted by Anna at 11:49pm | Get Permalink