Click here to Skip to main content
Click here to Skip to main content

Automating Project Settings in Visual studio using Macros

By , 18 Apr 2007
Rate this:
Please Sign up or sign in to vote.

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. Smile | :)

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. Wink | ;) 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. Smile | :)

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nibu babu thomas
Software Developer Microsoft
India India
My Technical Blog
 
I am a computer programmer who started his career as a Java programmer in 2000. Wrote a game and several applications like editors, DB apps etc in Java. My fascination for programming went on a high in the following years. I did a post graduate diploma of 2 years because of which I got introduced to a plethora of Languages. My favorite at that time was VB because it was way too easy to program, intelli-sense was too cool.
 
I joined an NGO in 2002 as part time computer programmer. Primary responsibility was to develop their website and to write apps for them in VB. During this process I got introduced to DHTML, CSS, HTML, JavaScript, wow had hell of a time. Learned and learned and learned during this time. I got into Visual C++ because of my post graduation in computer applications. I was forced into this powerful language but how lucky I am. Initially I was scared of CreateFont API Wink | ;) but now its a piece of cake. Smile | :)
 
As of now I'm working as Visual C++ engineer with Microsoft. My passion for this language never ends. Its the raw power of the language, the kind of performance and flexibility it provides, that keeps me motivated to continue working in this language. Started working in VC6 and all through till the latest version of Visual C++. Smile | :)
 
I'm part of the Microsoft Developer Support - Programming Languages Team. Enjoying every day.
Follow on   Twitter

Comments and Discussions

 
GeneralProject options change Pinmembervek14@lycos.com16-Sep-07 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 18 Apr 2007
Article Copyright 2007 by Nibu babu thomas
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid