在VS2015宏中找不到VCCLCompilerTool(使用Visual Commander)

时间:2015-12-02 23:54:15

标签: c++ visual-studio-2015 visual-studio-macros

我正在使用Visual Commander(来自https://vlasovstudio.com/visual-commander/)移植一些宏来操作从VS2010到VS2015的C ++项目。事情似乎工作正常(我可以访问解决方案,创建VCProjects,循环配置等),但我有一个问题 - 在VS2010中有效的调用:

cfg.Tools("VCCLCompilerTool")

...在VS2015中失败,其中包含" System.MissingMemberException:找不到类型' VCCollectionShim'的默认成员。"。错误,表明该集合没有" VCCLCompilerTool"项目

有谁知道如何解决这个问题?或者另一种方法来访问配置的C ++编译器设置? (除了手动解析vcxproj XML文件外。)

如果我打印出cfg.Tools中每个项目的ToString()值,我只会看到&垫片'例如' Microsoft.VisualStudio.Project.VisualC.VCProjectEngine.VCCLCompilerToolShim'。

如果我在一个真实的VS2010宏(它可以工作)中做同样的事情,那么每个项目都显示为' System .__ ComObject'。

在互联网上搜索表明“填充”错误意味着代码正在访问错误版本的VCProjectEngine或VCProject程序集。我确实安装了VS2005和VS2010以用于其他项目,但我暂时重命名了这些DLL的旧版本所在的每个地方,但仍然遇到同样的问题。也许它仍然从其他地方获得它,比如GAC?

我尝试将https://msdn.microsoft.com/en-US/library/microsoft.visualstudio.vcprojectengine.ivccollection.item(v=vs.140).aspx的IVCCollection.Item方法文档中的示例插入到Visual Commander命令中,并得到相同的结果。

Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic

Imports System

Imports System.Diagnostics
Imports Microsoft.VisualStudio.VCProjectEngine
Imports System.Text

Public Class C
    Implements VisualCommanderExt.ICommand

    Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
        EnablePREfastExample(DTE)
    End Sub

Sub EnablePREfastExample(ByVal dte As DTE2)
    Dim prj As VCProject
    Dim cfgs, tools As IVCCollection
    Dim cfg As VCConfiguration
    Dim tool As VCCLCompilerTool
    Dim sb As New StringBuilder

    prj = CType(dte.Solution.Projects.Item(1).Object, _
      Microsoft.VisualStudio.VCProjectEngine.VCProject)

    cfgs = CType(prj.Configurations, _
      Microsoft.VisualStudio.VCProjectEngine.IVCCollection)

    cfg = CType(cfgs.Item(1), _
      Microsoft.VisualStudio.VCProjectEngine.VCConfiguration)

' This fails
    tool = CType(cfg.Tools("VCCLCompilerTool"), _
      Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)


    sb.Length = 0
    sb.Append("Current project PREfast setting: " _
      & tool.EnablePREfast & Environment.NewLine)
    sb.Append("Flag: " & tool.AdditionalOptions)
    MsgBox(sb.ToString)

End Sub
End Class

我将Visual Commander中References ...对话框中的值设置为" Microsoft.VisualStudio.VCProjectEngine"。 (我也尝试完全指定路径名,如" C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ PublicAssemblies \ Microsoft.VisualStudio.VCProjectEngine.dll",或完整的GAC名称比如“Microsoft.VisualStudio.VCProjectEngine,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”并得到了同样糟糕的结果。)

1 个答案:

答案 0 :(得分:1)

感谢Visual Commander的作者Sergey Vlasov,我找到了答案。他指出,示例代码的C#版本在VS2015中有效。 C#和VB代码在所讨论的行中略有不同。 C#代码是:

tool = 
  (Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
  tools.Item("VCCLCompilerTool");

而VB代码是:

tool = CType(cfg.Tools("VCCLCompilerTool"), _
  Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)

这个VB在VS2010中运行良好,但在VS2015中看起来,默认情况下,Tools集合不能再按名称索引。所以我只需要添加" .Item",它将VB代码更改为:

tool = CType(cfg.Tools.Item("VCCLCompilerTool"), _
  Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)

现在这个宏一切都很好。

相关问题