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

606 views Asked by At

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?

2

There are 2 answers

0
mackenir On BEST ANSWER

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
0
Ujjwal Singh On

Modified code from @mackenir 's Answer - for current project:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text
Imports Microsoft.VisualStudio.VCProjectEngine
' Add Reference to Microsoft.VisualStudio.VCProjectEngine: Project > Add Refrence

Public Module Build

Sub CheckBuild()

    Dim Prj As Object = DTE.ActiveSolutionProjects(0).Object
    If Prj Is Nothing Then Return
    Dim VPrj As VCProject = CType(Prj, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    If VPrj Is Nothing Then Return
    Dim Cfg As VCConfiguration = CType(VPrj.Configurations.Item("Debug|x64"), Microsoft.VisualStudio.VCProjectEngine.VCConfiguration)
    If Cfg Is Nothing Then Return
    Dim CTool As VCCLCompilerTool = CType(Cfg.Tools("VCCLCompilerTool"), Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
    If CTool Is Nothing Then Return
    Dim LTool As VCLinkerTool = CType(Cfg.Tools("VCLinkerTool"), Microsoft.VisualStudio.VCProjectEngine.VCLinkerTool)
    If LTool Is Nothing Then Return

    CTool.DebugInformationFormat = debugOption.debugDisabled
    LTool.GenerateDebugInformation = "false"
    LTool.OptimizeReferences = optRefType.optNoReferences
    LTool.EnableCOMDATFolding = optFoldingType.optNoFolding

    ExecuteCommand("Build.BuildSelection")

End Sub