2015-12-02 37 views
0

我使用Visual Commander(從https://vlasovstudio.com/visual-commander/)將一些宏用於操作VS2010中的C++項目到VS2015。事情似乎工作確定(我可以通過配置接入解決方案,創造VCProjects,循環等),但我有一個問題 - 這曾在VS2010電話:在VS2015宏中找不到VCCLCompilerTool(使用Visual Commander)

cfg.Tools("VCCLCompilerTool") 

...在VS2015失敗,與「System.MissingMemberException:找不到類型爲'VCCollectionShim'的默認成員」。錯誤,表明該集合沒有「VCCLCompilerTool」項目。

有誰知道如何解決這個問題?或者另一種方式來訪問配置的C++編譯器設置? (除了手動解析vcxproj XML文件)

如果我打印出cfg.Tools中每個項目的ToString()值,我只會看到'shims',例如'Microsoft.VisualStudio.Project.VisualC.VCProjectEngine .VCCLCompilerToolShim」。

如果我在一個真正的VS2010宏(它工作)中做同樣的事情,每個項目顯示爲'System .__ ComObject'。

在互聯網上搜索表明,'shim'錯誤意味着代碼正在訪問錯誤版本的VCProjectEngine或VCProject程序集。我確實安裝了VS2005和VS2010供其他項目使用,但我暫時將這些DLL的舊版本所在的每個地方重新命名,並仍然遇到同樣的問題。也許它仍然從其他地方獲得它,比如GAC?

我試圖從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.VisualStudio.VCProjectEngine.dll」,或完整的GAC名稱,如「Microsoft.VisualStudio .VCProjectEngine,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a「並得到了同樣的錯誤結果。)

回答

1

感謝視頻指揮官的作者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,由工具集可以不再按名稱索引默認。所以我只需要添加「.Item」,它將VB代碼更改爲:

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

現在一切正常,這個宏。