Search code examples
visual-studio-2008visual-studio-macros

How to create a VS2008 macro to alter C++ project settings?


I want to write a Visual Studio macro that switches off optimisations for a given C++ project. Does anyone have sample code to alter a project setting? And assuming that project settings are exposed as some sort of dictionary of values, what are the keys and value values for a project's C++ and Linker optimisations?


Solution

  • Visual C++ Project Model:

    http://msdn.microsoft.com/en-us/library/2eydyk57.aspx

    Some hacked-together macro code:

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports System.Diagnostics
    Imports Microsoft.VisualStudio.VCProjectEngine
    Imports System.Text
    
    
    Public Module RecordingModule
    
        Sub DisableOptimizationsInAllProjectsReleaseWin32()
            For i = 1 To DTE.Solution.Projects.Count
                Dim proj As Object = DTE.Solution.Projects.Item(i).Object
                On Error Resume Next
                Dim prj As VCProject = CType(proj, Microsoft.VisualStudio.VCProjectEngine.VCProject)
                If Not prj Is Nothing Then
                    Dim cfgs As IVCCollection = CType(prj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
                    For c = 1 To cfgs.Count
                        Dim cfg As VCConfiguration = CType(cfgs.Item(c), Microsoft.VisualStudio.VCProjectEngine.VCConfiguration)
                        If cfg.Name = "Release|Win32" Then
                            Dim tool As VCCLCompilerTool = CType(cfg.Tools("VCCLCompilerTool"), Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
                            If Not tool Is Nothing Then tool.Optimization = optimizeOption.optimizeDisabled
    
                            Dim linker As VCLinkerTool = CType(cfg.Tools("VCLinkerTool"), Microsoft.VisualStudio.VCProjectEngine.VCLinkerTool)
                            If Not linker Is Nothing Then
                                linker.EnableCOMDATFolding = optFoldingType.optFoldingDefault
                                linker.LinkTimeCodeGeneration = LinkTimeCodeGenerationOption.LinkTimeCodeGenerationOptionDefault
                                linker.OptimizeReferences = optRefType.optReferencesDefault
                                linker.InlineFunctionExpansion = "0"
                                linker.EnableIntrinsicFunctions = "false"
                                linker.FavorSizeOrSpeed = "0"
                                linker.WholeProgramOptimization = "false"
    
                            End If
                            Exit For
                        End If
                    Next
                End If
            Next
        End Sub
    End Module