March 9, 2021
Hot Topics:

Controlling Project and File Properties with C++ Macros

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

In previous columns, I introduced you to the basics of writing Visual Studio macros in C++—well, to be accurate, writing a library in C++ that provides all the functionality for macros. I showed you how to insert text into a file being edited, and how to work with the code model that represents classes, interfaces, functions, and the like within your project.

In this installment, I tackle the project model. The particular task my macro will perform is changing a file (within a managed project) from managed (/clr) to unmanaged. This is something you might do for performance reasons, creating a mixed executable. When you make this change in Solution Explorer, you have to make a companion change, turning off precompiled headers. The macro does both steps. I'll leave it as an exercise for you to write the opposite macro that puts the file back to unmanaged.

Sample Project

I created an unmanaged console application and added a class to it (kept in a separate file). The class is called Person: the header is in Person.h and the implementation is in Person.cpp. I plan to flip Person.cpp back and forth between managed and unmanaged using the macro. Here's Person.h:

class Person
   int number;
   char code;
   Person(int n, char c) ;
   int getnumber(); 

You can guess what the two functions look like, and you might be tempted to write them inline in the .h file. But think what will happen when you #include that .h file into a .cpp file that is being compiled /clr: you will get MSIL versions of the functions. That's why I put the implementations into a separate file. That file will then be compiled to MSIL or native code according to the properties you've set for it. And after all, in real life, if you're flipping a file back to native code for performance reasons, it's going to have a great deal of code in it and not these little "demo code" examples. The macros don't care how much code they work on, so I wrote small examples. Here is Person.cpp:

#include "StdAfx.h"
#include ".\person.h"

Person::Person(int n, char c)
   number = n;
   code = c;

int Person::getnumber()
   return number;

I then wrote a really simple main():

#include "stdafx.h"

#include <iostream>
using namespace std;

#include "Person.h"

int _tmain(int argc, _TCHAR* argv[])
   Person p1(1,'a');
   Person p2(2,'q');
   cout << "total of the numbers: " <<
      p1.getnumber() + p2.getnumber() << '\n';
   return 0;

So far, this is all unmanaged code and has no .NET part to it. I built and ran it to make sure nothing weird was going on, and then used Solution Explorer to make the entire project managed (/clr). I then built and ran it again to make sure it still worked. This should be familiar (if you've read my head-spinning columns) as the "xcopy port" to the CLR.

Page 1 of 2

This article was originally published on June 16, 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