Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > Getting the VCProjectEngine in a C++ Package
Ask a questionAsk a question
 

AnswerGetting the VCProjectEngine in a C++ Package

  • Monday, October 19, 2009 5:07 PMBarry M Tannenbaum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I'm working on a package for VS2005 written in C++, and am having trouble getting an instance of the VCProjectEngine object.  I *think* something like the following should work, but QueryCachedService is failing with E_NOINTERFACE.  Here's the command handler.  It's the only thing I've customized in a wizard-generated package:

    #include "StdAfx.h"
    #include "Package.h"
    //#import <VCProjectEngine.dll> rename ("PropertySheet", "VCPropertySheetX")
    #import "C:\Program Files\Microsoft Visual Studio 8\VC\vcpackages\VCProject.dll" raw_interfaces_only raw_native_types  
    #import "C:\Program Files\Microsoft Visual Studio 8\VC\vcpackages\VCProjectEngine.dll" raw_interfaces_only raw_native_types rename("PropertySheet", "__PropertySheet")  
    using namespace VCProjectEngineLibrary;
    
    
    // Command handler called when the user selects the "My Command" command.
    void CVSAnnotationPackage::OnMyCommand(CommandHandler* /*pSender*/,
                                           DWORD /*flags*/,
                                           VARIANT* /*pIn*/,
                                           VARIANT* /*pOut*/)
    {
        HRESULT hr;
        CComPtr<VCProjectEngine> spEngine;
    
        hr = this->GetVsSiteCache().QueryCachedService<VCProjectEngine, __uuidof(VCProjectEngine)>(&spEngine);
    
    	if( FAILED( hr ) || !spEngine )
        {
            OutputDebugString (L"Failed to create VCProjectEngine!\n");
            return;
        }
    
        // Get the collection of VC Projects
        CComPtr<IDispatch> spDispProjects;
        hr = spEngine->get_Projects(&spDispProjects);
    
        CComQIPtr<IVCCollection> spCollProjects = spDispProjects;
        LONG nProjects;
        hr = spCollProjects->get_Count (&nProjects);
    }
    

Answers

  • Wednesday, October 21, 2009 2:56 AMEd DoreMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Barry,

    I don't think the VCProjectEngine is surfaced as a global service. But you should be able to retrieve the VCProject  easily enough, given you have an IVsProject. The object that implements IVsProject also implements IVsHierarchy. Cast that interface to an IVsHierarchy, and try calling IVsHierarchy.GetProperty with VSITEMID_ROOT and VSHPROPID_ExtObject. This returns the IDispatch interface correlates to the EnvDTE.Project interface. From there you should be able to cast the Project.Object property to a VCProject interface.

    Sincerely,
    Ed Dore

All Replies

  • Monday, October 19, 2009 5:28 PMBarry M Tannenbaum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    P.S.  If I can get from an IVsProject to a VCProject, that would also solve my problem.  But I don't see any way to get there from here...

    Thanks for the help.

         - Barry
  • Wednesday, October 21, 2009 2:56 AMEd DoreMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Barry,

    I don't think the VCProjectEngine is surfaced as a global service. But you should be able to retrieve the VCProject  easily enough, given you have an IVsProject. The object that implements IVsProject also implements IVsHierarchy. Cast that interface to an IVsHierarchy, and try calling IVsHierarchy.GetProperty with VSITEMID_ROOT and VSHPROPID_ExtObject. This returns the IDispatch interface correlates to the EnvDTE.Project interface. From there you should be able to cast the Project.Object property to a VCProject interface.

    Sincerely,
    Ed Dore
  • Wednesday, October 21, 2009 8:24 PMBarry M Tannenbaum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Ed.  I'll give that a try.

         - Barry
  • Thursday, October 22, 2009 1:04 PMBarry M Tannenbaum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    P.S.  I worked around this problem by getting the DTE and using the automation objects.  Is there any reason to go back to traversing the hierarchy instead?

         - Barry
  • Thursday, November 05, 2009 7:11 PMBarry M Tannenbaum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Ed.  That did it.  It's a pity there isn't a more direct path from the IVsHierarchy object to the VCProject.

         - Barry