NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Adding Support for Managed Extensions for C++ to an Existing Application

One of the strongest reasons for using Managed Extensions for C++ is the ease in converting existing C++ applications to Managed Extensions applications. The procedure is simple and straightforward and consists of three steps:

Modifying the Existing Project Settings

The first step is to modify the compiler options by adding the /com+ option and recompile the target application. The /com+ option enables support for Managed Extensions and forces a link to the proper library.

To modify the project settings

  1. Load the target project into Visual Studio.
  2. From the Solution Explorer, right-click the project node and click Property Pages.
  3. Click the C/C++ folder in the left pane
  4. Click General folder under C/C++
  5. Set the Enable NGWS Runtime property to Assembly Support (/com+)
  6. Click OK and rebuild the project.

Once the project has been rebuilt, support for Managed Extensions is now available.

Employing New Managed Extensions Functionality in Existing Applications

Once the target application has been built with Managed Extensions support, all NGWS runtime features, from managed objects to NGWS frameworks base classes (such as String), can be easily added. For a full list of these features, see Managed Extensions for C++.

For the purposes of this article, it is sufficient to demonstrate this step using the NGWS frameworks base classes String and Console.

The following code declares two instances of the String class and a managed array of int type:

String MyLiteral= L"Current array values";
String MyLiteral2= L"Current array values (after initialization)";
__gc int a1[] = new __gc int[20];
Note   Because the a1 array is a managed array the managed operator new is used to declare and initialize the memory to 0.

After the variables have been declared, an instance of the NGWS frameworks Console class is created and the current values (all equal to 0) in the array is printed using the Write method of the Console class and the Length method of the Array class:

Console::Write(MyLiteral);
Console::Write('\n');
for (i=0; i < a1->Length; ++i)   // Use the Length member
{
   Console::Write(a1[i]);
   Console::Write(L' ');
}

Finally, the array is reinitialized and the values are again printed:

for (i=0; i < 20; ++i)
   a1[i] = i;

for (i=0; i < a1->Length; ++i)   // Use the Length member
{
   Console::Write(a1[i]);
   Console::Write(L' ');
}

Examining the example code reveals some of the benefits of using Managed Extensions in a C++ application:

Building and Testing for Regression

The final step is achieving an error-free build of the application and testing of the new functionality.

See Also

Managed Extensions for C++