Nerdworks logo "The nerd shall inherit the earth."

Nerdworks Blogorama

Nerdspeak

Turn off VS.NET 2005 deprecation
Technobabble
6/25/2006 3:13:32 PM

If you've been compiling projects created using earlier versions of VisualStudio in VisualStudio 2005 then you have most certainly noticed the new security warnings that get displayed whenever an in-secure CRT routine is invoked from your code. If you call strcpy for instance, you'd see this:

    warning C4996: 'strcpy' was declared deprecated
    This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use
    _CRT_SECURE_NO_DEPRECATE. See online help for details.

If you're writing new code then it is generally a good idea to listen to this warning and change your code. If you are building an existing project however (an open source project for example) then you're likely to get hundreds of C4996 warnings. One straightforward way of turning them off is to disable deprecation warnings by defining _CRT_SECURE_NO_DEPRECATE.

If you've got a large solution with 15-20 projects and 2-3 build configurations in each then defining this symbol for each project can be one seriously daunting task. In a 10 project solution with each project having a "Debug" and a "Release" configuration for instance you'd have to define this symbol in the project properties dialog 20 times (10 * 2)! I found myself having to do this often enough to warrant the writing of a small VisualStudio macro to do the job. This macro captures the preprocessor definitions from an input box and adds it to all the configurations of each project that is currently selected in the solution explorer. Here's the macro definition:

Public Sub AddPreprocessorMacroToAllProjects()

    '
    ' check whether at least one project has been selected
    '
    If DTE.ActiveSolutionProjects.Length = 0 Then
        MsgBox("Please select the Visual C++ project(s) " + _
            "to which you would like a macro to be added.")
        Exit Sub
    End If

    '
    ' get the macro names and values
    '
retry:
    Dim macro As String
    macro = InputBox("Please enter one or more macros " + _
        "(e.g. _WIN32_WINNT=0x0500; WINVER=0x0500)", _
            "Enter Macros").Trim()
    If macro.Length = 0 Then
        If MsgBox("An empty macro was entered.  Retry?", _
                MsgBoxStyle.YesNo, _
                "Wrong macro") = MsgBoxResult.Yes Then
            GoTo retry
        Else
            Exit Sub
        End If
    End If

    '
    ' now iterate through each project in the array and add the
    ' macro to all the configurations of all visual c++ projects
    '
    Dim i As Integer
    Dim project As Project
    Dim VCProjectKind As String = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"

    For i = 0 To DTE.ActiveSolutionProjects.Length - 1
        project = DTE.ActiveSolutionProjects(i)

        '
        ' is it a visual c++ project?
        '
        If project.Kind = VCProjectKind Then
            Dim vcproj As Microsoft.VisualStudio.VCProjectEngine.VCProject
            vcproj = project.Object
            Dim j As Integer

            '
            ' iterate through each configuration on this project
            '
            For j = 1 To vcproj.Configurations.Count
                Dim config As Microsoft.VisualStudio.VCProjectEngine.VCConfiguration
                config = vcproj.Configurations(j)

                '
                ' now add the macro to the compiler settings of this configuration
                '
                Dim cl As Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool
                cl = config.Tools("VCCLCompilerTool")
                cl.PreprocessorDefinitions = cl.PreprocessorDefinitions + "; " + macro
            Next
        End If
    Next

    MsgBox("Done.")

End Sub

Feel free to use it if you find it useful!

 

Please fill this form and click on the "Submit" button to post a comment. All fields except the comment box are optional. You don't have to give me your name and email, but if you do then that might allow me to follow up with you on your comment. Also, I won't publish your email address here or anywhere else.

 
Your Name :
Your Email :
Your Comment :
   

What in your opinion do you get when you multiply the number 5 by the number 2?

Your answer will help me figure out whether you are human or a spam bot. If you're a spam bot I hope your kernel core dumps and your CPU bursts into flames.

   

Please click here to go back to the blog.

blogorama home
about this blog
email the author
where on earth am i?
subscribe to mailing list
feeds Use these links for feed syndication
rss  |  atom
by category
technobabble (54)
philosophical crud (3)
irrelevant stuff (7)
archive
september, 2011 (7)
july, 2011 (3)
june, 2011 (2)
may, 2011 (3)
april, 2011 (1)
march, 2011 (1)
february, 2011 (1)
february, 2010 (1)
october, 2009 (1)
september, 2009 (1)
july, 2009 (5)
march, 2009 (2)
august, 2008 (2)
march, 2008 (1)
january, 2008 (1)
september, 2007 (2)
april, 2007 (1)
february, 2007 (2)
december, 2006 (1)
october, 2006 (1)
september, 2006 (4)
august, 2006 (3)
july, 2006 (4)
june, 2006 (3)
may, 2006 (6)
april, 2006 (2)
recent entries
IE9 web cast / Chen...
Partial function ap...
Web Camps, Virtual...
176874 hits