March 5, 2021
Hot Topics:

Macros for C++, in C++

  • By Kate Gregory
  • Send Email »
  • More Articles »

Where are the COM and Project tabs? More distressingly, where is the Browse button? How are you going to find the new assembly to add the reference to it? And while you're asking questions like that, just what makes assemblies appear on that .NET tab anyway? It's tempting to think they are assemblies from the GAC, but you can't add a reference to an assembly that's only in the GAC, so that can't be it. It turns out the assemblies in that list are all in a particular folder on your hard drive: Visual Studio Install Path\Common7\IDE\PublicAssemblies. You can copy anything you like there, and it will appear on that dialog. You won't care when you are building ordinary projects, but it's vital information for a macro project that uses other assemblies.

I copied the assembly from the class library project to the PublicAssemblies folder, and then flipped back to Visual Studio in which the Macro Explorer was open and added the reference. Here's the VB code for the macro:

Imports EnvDTE
Imports System.Diagnostics

Public Module Editing
   Sub Braces()
   End Sub
End Module

I only had to write one line of this: the call to Braces. I can handle having that much VB in my C++ projects without complaining.

Getting the Macro Onto a Toolbar

There are lots of ways to execute a macro, but getting it onto a toolbar is handy. In Visual Studio, right-click any toolbar and then click Customize. On the Toolbar tab, click New, and give the toolbar a name (I called mine Kate). On the Commands tab, select Macros in the category list (it's second from the bottom) and MyMacros.Editing.Braces (or whatever you called your macro) in the commands list, like this:

Drag the MyMacros.Editing.Braces entry onto the empty toolbar floating near the dialog; then drag the toolbar up with the other toolbars. Close the Customize dialog. Open a file of code, and type this line:

if (a == b)

Click the toolbar button and the braces should appear following your if statement. That's a time-saver, right?

Associating a Keystroke with the Macro

Mousing all the way up to the toolbar to get your braces inserted for you isn't as convenient as it could be. That same Customize dialog that got the button onto the toolbar also is used for setting keyboard shortcuts. On the Commands tab, click Keyboard. It doesn't matter which command you select before clicking. On the Options dialog that appears, scroll through the alphabetical list to Macros.MyMacros.Editing.Braces, select it, click in the Press Shortcut Keys box, and type a shortcut key that seems good to you. Alt+B for braces made sense to me. Click OK. If it's your first shortcut, let Visual Studio rename the Default Settings to something else: mine is called Kate Default Settings now.

The Development Cycle

There is one annoyance when you develop your macros this way. After you've run the macro, you are likely to decide to change it a bit. So, you change the C++ code and rebuild the assembly, and then copy it over to PublicAssemblies. Unfortunately, that copy will fail because the assembly is in use. You'll need to close Visual Studio so that it will release the assembly. Then, you can copy the assembly again and start Visual Studio to test your changes. It's a little awkward, so be prepared for it.

So there you have it: a class library in C++ that works with the DTE object, a tiny macro in VB that uses the class library, a neat trick to add the reference to the macro project, and you've got a macro for C++, written in C++. You can adapt this to create a variety of handy macros, each calling a different method in the class library. I've got more macro and add-in work ahead, so stay tuned.

About the Author

Kate Gregory is a founding partner of Gregory Consulting Limited (www.gregcons.com). In January 2002, she was appointed MSDN Regional Director for Toronto, Canada. Her experience with C++ stretches back to before Visual C++ existed. She is a well-known speaker and lecturer at colleges and Microsoft events on subjects such as .NET, Visual Studio, XML, UML, C++, Java, and the Internet. Kate and her colleagues at Gregory Consulting specialize in combining software develoment with Web site development to create active sites. They build quality custom and off-the-shelf software components for Web pages and other applications. Kate is the author of numerous books for Que, including Special Edition Using Visual C++ .NET.

Page 2 of 2

This article was originally published on April 30, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date