Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Automating Project Settings in Visual studio using Macros

0.00/5 (No votes)
18 Apr 2007 1  
Set of macros that helps in automating project settings of a project.

Download ADVANCEDNIBUMACROS.zip - 1.4 KB

Introduction

When working with Visual Studio 6 I always had to fix issues related to project settings from customers. Mainly this kind of issues occur when you have a multiple project configurations. It's a hard chore to open up each project's setting and fix issues.

For eg: To type in UNICODE and _UNICODE in all projects and in each project's configuration is a hard chore, to change warning settings to four, to enable debug information for release builds. To enable RTTI for all projects in a workspace etc.

With project settings macros you don't have to do this, just one mouse click and it's done. :)

Background

Well then this thought came into my mind of recording macros for automating project settings related issues, but this failed miserably and of course you know why. ;) Project settings are done through the UI provided by VS and macros work till that point. When you click on that macro only the project settings dialog will pop up.

Thus after further digging of MSDN I found out that we can automate tasks in the Visual C++ development environment by using Developer Studio objects with VBScript macros and Developer Studio add-ins.

Developer studio Add in objects

I won't talk about Developer studio add-ins which in itself is a big subject but I would like to tell you that you can create an add-in for this purpose too.

Now over to VBScript Developer studio add in objects.

Following is the Developer studio object hierarchy ( From MSDN )

Application
  Application (Application is its own parent)
    Projects
      Project
       Configurations
         Configuration
           Configurations (circular)
    Documents
      Document
        Window (for "Generic" window types)
      TextDocument
        TextSelection
        TextWindow
        Window (for "Text" window types)
    TextEditor
    Windows
    Debugger
    Breakpoints
       Breakpoint

Top most object is the Application Object. For addins this will be IApplication interface. Now you might have a got an idea on how to do this if you know something about VBScript. Forgive me for using VB.

For eg:
To access all projects in an application you would use

' This will give a project collection

Set ProjectColls = Application.Projects
'To access individual projects in the above project Collection we will use

ProjectColls.Project.

Look up the above object hierarchy and you will understand what's going on.

Now the question arises how to get all configurations in a project. As you guessed it's simple...

' Get all configurations for a given project

Set ProjectConfigs = ProjectColls.Project.Configurations

It's easy to get each configuration in a configuration collection

' Loop through each configuration

For each Config in ProjectConfigs
    ' Do something with this configuration

    MsgBox Config.Name
Next

Here is a simple RebuildAll macro...

Sub ReBuildAll
    ' Get all projects in the workspace
    Set ProjectsCols = Application.Projects
    For Each Proj in ProjectsCols
        if Proj.Type = "Build" then
             Set ConfigCols = Proj.Configurations
             for each Config in ConfigCols
                 RebuildAll Config
             Next
       End if
   Next
End Sub


Appplication Events

Another interesting prospect is events. The list of events for an Application object is given below...

  1. BeforeApplicationShutDown
  2. NewDocument
  3. BeforeBuildStart
  4. NewWorkspace
  5. BeforeDocumentClose
  6. WindowActivate
  7. BuildFinish
  8. WindowDeactivate
  9. DocumentOpen
  10. WorkspaceClose
  11. DocumentSave
  12. WorkspaceOpen

A sample event handler is given below

Sub Application_BeforeBuildStart()
   ' Insert code to handle the event here
   MsgBox "Go ahead and build, let your project compile with zero errors. ;P"
End sub

Did you see the word Application in Bold. You should have the application word as prefix to Application event handlers

One more example...

Sub Application_BuildFinish( nNumErrors, nNumWarnings )
     ' Display a message box if there are any errors or warnings.

     If nNumErrors <> 0 Or nNumWarnings <> 0 Then
          MsgBox "You have " & nNumErrors & " errors and " & nNumWarnings & " warnings"
     End If
End sub

To get hold of all breakpoints in a project do this... ( From MSDN )

Dim myBreakpoint
For Each myBreakpoint in Debugger.Breakpoints
     ' Access myBreakpoint here.
     ' For example:
      MsgBox myBreakpoint.PassCount
Next

There are other breakpoint related functions...

AddBreakpointAtLine
RemoveBreakpointAtLine

There are some other object that you might be interested in. The ones that I found good are

Debugger
TextEditor
TextDocument
TextSelection

Although to be honest I didn't get the time to have a look at them in depth.


Changing project settings

Now lets come to the purpose of this article i.e how to change project settings using this Developer studio object through VB script.

I have already explained the process of how we are doing this, so I will just share the code here...

Sub ProjectSettingsMacro()
    ' Get all projects in the workspace

    Set ProjectsCols = Application.Projects
    for each Proj in ProjectsCols
       if Proj.Type = "Build" then
          Set ConfigCols = Proj.Configurations
          
          for each Config in ConfigCols
             
         ' Do release specific configuration

             if InStr( Config.Name, "Release" ) > 0 then
                 ' Set optimization to minimize size

                 Config.AddToolSettings "cl.exe", "/O1"
                 ' Generate PDB file

                 Config.AddToolSettings "cl.exe", "/Zi"
                 ' Enable Multithreaded DLL

                 Config.AddToolSettings "cl.exe", "/MD"
            elseif InStr( Config.Name, "Debug" ) > 0 then
                 ' Enable Multithreaded Dll debug option

                 Config.AddToolSettings "cl.exe", "/MDd"
            end if

        
            'Enable unicode settings

            Config.AddToolSettings "cl.exe", "/DUNICODE /D_UNICODE"
            
            ' Remove precompiled header file support

            Config.RemoveToolSettings "cl.exe", "/Yu""stdafx.h"""
            
            ' Set warning level to 4 and treat any warnings as error

            Config.AddToolSettings "cl.exe", "/W4 /WX"

            
            ' Remove browse file information

            Config.RemoveToolSettings "cl.exe", "/Fr /FR"
            
            ' Enable no default library option

            Config.AddToolSettings "link.exe", "/NODEFAULTLIB"
            
            ' Enable debug information

            Config.AddToolSettings "link.exe", "/DEBUG"
            
            ' Disable profiling

            Config.RemoveToolSettings "link.exe", "/profile"

          
          Next ' For 

       end if ' Fi

    Next ' For

End Sub


How to...

Lets look at the process of incorporating this into our developer studio...

  1. Download given macro file to this directory...
  2. %YourMSDEVDirectory%/Common\MSDev98\Macros
  3. Goto Tools->Customize->Select the macrofile.

Screenshot - Customizedialog.gif

4 Now goto Tools->Macro->Click on the macro file combo box... Select the
downloaded file.

Screenshot - Macroadding.gif

Now select any of the macros and click on Run button

Screenshot - Macrolist.gif

To test this macro start a new project and run macro (ProjectSettingsMacro). Open project settings dialog to see the result.


Procedure to create a new macro for yourself...

Open MSDEV

Now in the same Macro dialog Type a new name for the Macro in Macro name field. Now click on the edit button.

Just paste the above code there. Now when this is done you can also assign a keyboard shortcut to your macro. In the same dialog click on KeyStrokes. Specify a new shortcut key. So now whenever you press this shortcut key the macro will run.

Now create a fresh project and press the shortcut key to see whether it works or not. Saves a lot of time for me.

Points of Interest

Developer studio objects are real powerful. Addins are also powerful but Macros are powerful in the sense that it's very simple compared to the tasks it does.

Conclusion

There are some more objects remaining which I can try to add to this article. Search MSDN with above keywords to bump into pretty interesting and powerful tools.

And of course there is nothing geekish in this article, just using some tools provided by MS.

Hope this benefits you. :)

Note: I haven't tested these ideas with VS7 and VS8 but the ideas should remain the same. I have only tested this in VS6 and this works real cool.

History

Created on 4/11/2007
Modified on 4/18/2007 -- Added some more demonstration pictures.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here