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

Cannot find VCCLCompilerTool in VS2015 macro (using Visual Commander)

本文关键字:使用 Visual Commander VCCLCompilerTool VS2015 找不到      更新时间:2023-10-16

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

cfg.Tools("VCCLCompilerTool")

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

有人知道怎么解决这个问题吗?或者以另一种方式访问配置的C++编译器设置?(除了手动解析vcxproj XML文件。)

如果我打印出cfg中每个项的ToString()值。工具,我只看到"垫片",如"Microsoft.VisualStudio.Project.VICProjectEngine.VCCLCompilerToolShim".

如果我在一个真正的VS2010宏中也这样做(有效),则每个项都显示为"System"__ComObject"。

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

我尝试插入IVCCollection.Item方法文档中的示例https://msdn.microsoft.com/en-US/library/microsoft.visualstudio.vcprojectengine.ivccollection.item(v=vs.140).aspx转换为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.Visual Studio.VCProjectEngine.dll",或GAC全名,如"Microsoft.VisualStudio.VCProjectEngine,Version=14.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a",并得到相同的错误结果。)

感谢《视觉指挥官》的作者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)

现在这个宏一切都很好。