The .NET Managed Extensions to C++

The .NET Managed Extensions to C++

Overview

Microsoft’s .NET Framework defines an environment that supports the development and execution of highly distributed, component-based applications. It enables differing computer languages to work together and provides for security, program portability, and a common programming model for the Windows platform. Although the .NET Framework is a relatively recent addition to computing, it is an environment in which many C++ programmers will likely be working in the near future.

Microsoft’s .NET Framework provides a managed environment that oversees program execution. A program targeted for the .NET Framework is not compiled into executable object code. Rather, it is compiled into Microsoft Intermediate Language (MSIL), which is then executed under the control of the Common Language Runtime (CLR). Managed execution is the mechanism that supports the key advantages offered by the .NET Framework.
To take advantage of .NET managed execution, it is necessary for a C++ program to use a set of nonstandard, extended keywords and preprocessor directives that have been defined by Microsoft. It is important to understand that these extensions are not defined by ANSI/ISO Standard C++. Thus, code in which they are used is non-portable to other environments.
It is far beyond the scope of this book to describe the .NET Framework or the C++ programming techniques necessary to utilize it. However, a brief synopsis of the .NET managed extensions to C++ is given here for the benefit of those programmers working in the .NET environment. A basic understanding of the .NET Framework is assumed.

The .NET Keyword Extensions

To support the .NET managed execution environment, Microsoft adds the following keywords to the C++ language.
_ _abstract
_ _box
_ _delegate
_ _event
_ _finally
_ _gc
_ _identifier
_ _interface
_ _nogc
_ _pin
_ _property
_ _sealed
_ _try_cast
_ _typeof
_ _value
Each of these is briefly described in the following sections.

_ _abstract

_ _abstract is used in conjunction with _ _gc to specify an abstract managed class. No object of an _ _abstract class can be created. A class specified as _ _abstract is not required to contain pure virtual functions.

_ _box

_ _box wraps a value within an object. Boxing enables a value type to be used by code that requires an object derived from System::Object, which is the base class of all .NET objects.

_ _delegate

_ _delegate specifies a delegate, which encapsulates a pointer to a function within a managed class (that is, a class modified by _ _gc).

_ _event

_ _event specifies a function that represents an event. Only the prototype for the function is specified.

_ _finally

_ _finally is an addition to the standard C++ exception handling mechanism. It is used to specify a block of code that will be executed when a try/catch block is left. It does not matter what conditions cause the try/catch block to terminate. In all cases, the _ _finally block will be executed.

_ _gc

_ _gc specifies a managed class. Here, “gc” stands for “garbage collection” and indicates that objects of the class are automatically garbage collected when they are no longer needed. An object is no longer needed when no references to the object exist. Objects of a _ _gc class must be created using new. Arrays, pointers, and interfaces can also be specified as _ _gc.

_ _identifier

_ _identifier allows a C++ keyword to be used as an identifier. This is a special-purpose extension that will not be used by most programs.

_ _interface

_ _interface specifies a class that will act as an interface. In an interface, no function can include a body. All functions in an interface are implicitly pure virtual functions. Thus, an interface is essentially an abstract class in which no function has an implementation.

_ _nogc

_ _nogc specifies a non-managed class. Since this is the type of class created by default, the _ _nogc keyword is seldom used.

_ _pin

_ _pin is used to specify a pointer that fixes the location in memory of the object to which it points. Thus, an object that is “pinned” will not be moved in memory by the garbage collector. As a result, garbage collection does not invalidate a pointer modified by _ _pin.

_ _property

_ _property specifies a property, which is a member function that gets or sets the value of a member variable. Properties provide a convenient means to control access to private or protected data.

_ _sealed

_ _sealed prevents the class that it modifies from being inherited. It can also be used to specify that a virtual function cannot be overridden.

_ _try_cast

_ _try_cast attempts to cast one type of expression into another. If the cast fails, an exception of type System::InvalidCastException is thrown.

_ _typeof

_ _typeof obtains an object that encapsulates type information for a given type. This object is an instance of System::Type.

_ _value

_ _value specifies a class that is represented as a value type. A value type holds its own values. This differs from a _ _gc type, which must allocate storage through the use of new. Value types are not subject to garbage collection.

Preprocessor Extensions

To support .NET, Microsoft defines the #using preprocessor directive, which is used to import metadata into your program. Metadata contains type and member information in a form that is independent of a specific computer language. Thus, metadata helps support mixed-language programming. All managed C++ programs must import <mscorlib.dll>, which contains the metadata for the .NET Framework.
Microsoft defines two pragmas that relate to the .NET Framework. (Pragmas are used with the the #pragma preprocessing directive.) The first is managed, which specifies managed code. The second is unmanaged, which specifies unmanaged (that is, native) code. These pragmas can be used within a program to selectively create managed and unmanaged code.

The attribute Attribute

Microsoft defines attribute, which is the attribute used to declare another attribute.

Compiling Managed C++

At the time of this writing, the only compiler commonly available that can target the .NET Framework is the one supplied by Microsoft’s Visual Studio .NET. To compile a managed code program, you must use the the /clr option, which targets code for the Common Language Runtime.

Followers