Trying to cast a DTE project to a VCProject causes an exception saying DLL not registered

0

I am using VS 2017 to write C# code that uses the DTE API to generate a Visual Studio solution with a set of cpp (vcxproj) projects. I can create the sln and vcxproj files ok, using a vcxproj template (from the VS 2017 samples). But when I try to cast a EnvDTE.Project to a VCProject (namespace Microsoft.VisualStudio.VCProjectEngine), I get an error saying that the VCProject's interface is not registered:

{"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.VCProjectEngine.VCProject'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{DBF177F2-06DB-4A47-8AAD-C8E12BFD6C86}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155)."}

My C# project references extension DLL Microsoft.VisualStudio.ProjectEngine, which is where VCProject is defined, and it compiles fine. (Another available extension DLL is Microsoft.VisualStudio.VCProject, but it doesn't contain the VCProject itself, and referencing it has no effect for me).

My question is, is this normal behavior for EnvDTE/VCProject? If your C# program uses VCProject, is the program expected to first programmatically register the VCProject DLL if necessary, so it can run on any Windows machine? I've found other questions about the unregistered DLL exception, but the answer is always to register the DLL. Why would a referenced assembly need to be registered? My code is as follows.

using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.VCProjectEngine;

Type type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;
dte.SuppressUI = true;
EnvDTE100.Solution4 solution = (EnvDTE100.Solution4)dte.Solution;
solution.Create(@"C:\Test\", "Test");
// create the cpp project
string cppTemplateFile = @"C:\Test\Cpp\DLL.vstemplate";
solution.AddFromTemplate(cppTemplateFile, @"C:\Test", projNameCpp, false);

foreach (Project project in solution.Projects) 
{
    // this throws an exception about VCProject not registered:
    VCProject vcProject = project.Object as VCProject;

    // same exception happens with direct casting:
    //VCProject vcProject = (VCProject)project.Object;
}
c#
visual-studio
envdte
dllregistration
asked on Stack Overflow Oct 23, 2017 by Gordon Dakin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0