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
Set ProjectColls = Application.Projects
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...
Set ProjectConfigs = ProjectColls.Project.Configurations
It's easy to get each configuration in a configuration collection
For each Config in ProjectConfigs
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...
-
BeforeApplicationShutDown
-
NewDocument
-
BeforeBuildStart
-
NewWorkspace
-
BeforeDocumentClose
-
WindowActivate
-
BuildFinish
-
WindowDeactivate
-
DocumentOpen
-
WorkspaceClose
-
DocumentSave
-
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 )
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()
Set ProjectsCols = Application.Projects
for each Proj in ProjectsCols
if Proj.Type = "Build" then
Set ConfigCols = Proj.Configurations
for each Config in ConfigCols
if InStr( Config.Name, "Release" ) > 0 then
Config.AddToolSettings "cl.exe", "/O1"
Config.AddToolSettings "cl.exe", "/Zi"
Config.AddToolSettings "cl.exe", "/MD"
elseif InStr( Config.Name, "Debug" ) > 0 then
Config.AddToolSettings "cl.exe", "/MDd"
end if
Config.AddToolSettings "cl.exe", "/DUNICODE /D_UNICODE"
Config.RemoveToolSettings "cl.exe", "/Yu""stdafx.h"""
Config.AddToolSettings "cl.exe", "/W4 /WX"
Config.RemoveToolSettings "cl.exe", "/Fr /FR"
Config.AddToolSettings "link.exe", "/NODEFAULTLIB"
Config.AddToolSettings "link.exe", "/DEBUG"
Config.RemoveToolSettings "link.exe", "/profile"
Next
end if
Next
End Sub
How to...
Lets look at the process of incorporating this into our developer studio...
-
Download given macro file to this directory...
-
%YourMSDEVDirectory%/Common\MSDev98\Macros
-
Goto Tools->Customize->Select the macrofile.
4 Now goto Tools->Macro->Click on the macro file combo box... Select the
downloaded file.
Now select any of the macros and click on Run button
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.