Managed C
   HOME

TheInfoList



OR:

Managed Extensions for C++ or Managed C++ is a now-deprecated set of language extensions for
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the
.NET Framework The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
. These extensions were created by
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washin ...
to allow C++ code to be targeted to the Common Language Runtime (CLR) in the form of
managed code Managed code is computer program code that requires and will execute only under the management of a Common Language Infrastructure (CLI); Virtual Execution System (VES); virtual machine, e.g. .NET, CoreFX, or .NET Framework; Common Language Runt ...
, as well as continue to interoperate with native code. In 2004, the Managed C++ extensions were significantly revised to clarify and simplify syntax and expand functionality to include managed generics. These new extensions were designated
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
and included in Microsoft Visual Studio 2005. The term ''Managed C++'' and the extensions it refers to are thus deprecated and superseded by the new extensions.


History

Microsoft introduced Managed Extensions for C++ in
Microsoft Visual C++ Microsoft Visual C++ (MSVC) is a compiler for the C, C++ and C++/CX programming languages by Microsoft. MSVC is proprietary software; it was originally a standalone product but later became a part of Visual Studio and made available in both tri ...
2002 (MSVC++). Microsoft attempted to minimise the deviations between standard C++ and Managed Extensions for C++, resulting in core differences between the two being syntactically obscured. MSVC++ 2003 and 2005 also provided support for writing programs in Managed C++. In 2004, Managed Extensions for C++ was deprecated in favor of
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
, a second attempt by Microsoft at supporting programming for the Common Language Infrastructure using C++.


Design

''Managed'' refers to
managed code Managed code is computer program code that requires and will execute only under the management of a Common Language Infrastructure (CLI); Virtual Execution System (VES); virtual machine, e.g. .NET, CoreFX, or .NET Framework; Common Language Runt ...
that it is run in, or ''managed'' by, the .NET
virtual machine In computing, a virtual machine (VM) is the virtualization/ emulation of a computer system. Virtual machines are based on computer architectures and provide functionality of a physical computer. Their implementations may involve specialized h ...
that functions as a sandbox for enhanced security in the form of more runtime checks, such as buffer overrun checks. Additionally, applications written in Managed C++ compile to CIL—Common Intermediate Language—and not directly to native CPU instructions like standard C++ applications do. Managed C++ code could inter-operate with any other language also targeted for the
CLR CLR may refer to: * Calcium Lime Rust, a household cleaning-product * California Law Review, a publication by the UC Berkeley School of Law * Tube_bending, Centerline Radius, a term in the tubing industry used to describe the radius of a bend * Cen ...
such as C# and
Visual Basic .NET Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visua ...
as well as make use of features provided by the
CLR CLR may refer to: * Calcium Lime Rust, a household cleaning-product * California Law Review, a publication by the UC Berkeley School of Law * Tube_bending, Centerline Radius, a term in the tubing industry used to describe the radius of a bend * Cen ...
such as
garbage collection Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclabl ...
. This means Managed C++ occupies a unique position in the gallery of .NET languages. It is the only language that can communicate directly with .NET languages (such as C#, VB.NET) as well as native C++. The other .NET languages can only communicate with C++ code via PInvoke or COM. But since Managed C++ can communicate directly in both managed and standard C++ contexts, it is often used as a "bridge".


Functionality

Programs coded in Managed C++ provide additional functionality of the .NET Framework and the
CLR CLR may refer to: * Calcium Lime Rust, a household cleaning-product * California Law Review, a publication by the UC Berkeley School of Law * Tube_bending, Centerline Radius, a term in the tubing industry used to describe the radius of a bend * Cen ...
. Most notable of these is
garbage collection Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclabl ...
, which relieves the programmer of manual memory management. The garbage collector (GC) is handled by the CLR. Memory management is executed quite quickly, but for more performance critical applications, native, unmanaged code is most likely the preferred option. Managed C++ is geared towards object-oriented programming. A major difference between standard C++ and Managed C++ is that
multiple inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
is not supported, and a class managed under the CLR's garbage collector cannot inherit more than one class. This is because of a limitation of the CLR. Key features: * Extensible metadata: information provided to describe the structure and types of a managed component. It can be extended and reused to create software components. It is used heavily in C# and Visual Basic .NET * Garbage collection: the CLR is fully managed by a garbage collector for memory management automated by the CLR itself, i.e. the delete operator need not be called in managed C++ code. * Interoperability with .NET languages: code targeted for the .NET Framework produces
Microsoft Intermediate Language Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washingt ...
(MSIL, similar to Java bytecode) output and thus, modules compiled and components (assemblies rather) can be reused by other program components written in another language targeting the .NET Framework, such as JScript .NET, C#, Visual Basic .NET and other 3rd party languages for .NET. * Versioning: new methods and data members can be introduced into existing managed classes without breaking binary compatibility with existing client side software. * Binary headers: allows reusing of precompiled metadata; any .exe, .dll, .obj or .netmodule compiled into MSIL can be referenced from a C++ source file. * Buffer overflow protection - with the introduction of garbage collection into C++, Managed C++ is less prone to the common
buffer overflow In information security and programming, a buffer overflow, or buffer overrun, is an anomaly whereby a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory locations. Buffers are areas of memo ...
errors caused by the absence of data type checking in standard C++. The garbage collector helps to reduce (not completely though) the frequency of these errors. * .NET framework
Base Class Library The Standard Libraries is a set of libraries included in the Common Language Infrastructure (CLI) in order to encapsulate many common functions, such as file reading and writing, XML document manipulation, exception handling, application globa ...
- Managed C++ also has the potential to be less verbose than standard unmanaged code, since all managed function calls and inherited classes are derived from the .NET Framework Base Class Library (BCL, sometimes referred to as FCL or Framework Class Library), whose API provides TCP/IP networking capabilities, textual manipulation functions, data access (from ODBC to SQL), XML services (from XSD to XSL), GUI programming (Windows Forms), mail services (SMTP), cryptography (X509 Certificates and XML digital signatures), MSIL generation (essentially emitting instructions in MSIL), file I/O, manual manipulation of the CLR garbage collector and management information to manipulate the WMI console.


Advantages over native code

* Managed and unmanaged code can be mixed together in the same CLI assembly seamlessly. This allows the programmer to keep unmanaged code that cannot be ported over to the
.NET Framework The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
without re-writing it completely. Some ramifications of using this hybrid convention are present though. * Managed C++ is the only language that can contain unmanaged code and natively communicate with all other .NET languages. Managed C++ is thus very convenient for interoperability between programmers who use different languages, including those in the .NET theater and those who use standard C++.


Disadvantages compared to unmanaged code

* Managed C++ introduces a lot of new keywords and syntactic conventions that can impair the readability of code, especially if C++ code is included directly and interacts directly with Managed C++ code in the same assembly. * Managed C++ is superseded by
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
and thus obsolete as
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
has been standardized.


Disadvantages compared to fully managed code

* Managed C++ requires a slightly longer development time than other .NET languages that could be applied to projects that still produce the same results. The use of pointers may or may not be a requirement, because managed C++ has both value types (__value struct and __value class) and reference types (__gc struct and __gc class). * Managed C++ fully support
ASP.NET ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services. The name s ...
web applications, even though the development is more difficult than with other .NET languages, including some other third party languages. * Managed C++ includes only support for templates (for interoperability with native C++) but no support for generics (for interoperability with all the other .NET languages).
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
supports both templates (at compile time) and generics (at run time).


Examples

The following examples depict the use of Managed C++ as compared to standard C++: *(Global change) Existing C++ to be ported over the CLR must be appended with the following: //hello.cpp //new using directive #using //another using namespace directive. using namespace System; int main() A new preprocessor directive #using is required. In addition to that, more #using directives are required to import more libraries to use more namespaces in the Base Class Library, such as #using and using namespace System::Windows::Forms; to use Windows Forms. *To compile code to target the CLR, a new compiler option must be introduced. cl.exe hello.cpp /clr /clr enables any code referencing the .NET Framework to be compiled as CIL. *A class can be designated to be garbage collected via the __gc extension keyword. //gc.cpp #using __gc class gc ; int main() The preceding code can be compiled and executed without any fear of
memory leak In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object ...
s. Because class gc is managed under the garbage collector, there is no need to call the delete operator. To achieve the same with unmanaged code, the delete keyword is required: //nogc.cpp class gc ; int main() Notes: *A __gc designated class can have a constructor declared. *A __gc designated class can have a destructor declared. *A __gc designated class cannot inherit more than one class. (This is a limitation of the CLR) *A __gc designated class cannot inherit another class that is not __gc designated. *A __gc designated class cannot be inherited by another class that is not __gc designated. *A __gc designated class can implement any number of __gc interfaces. *A __gc designated class cannot implement an unmanaged interface. *A __gc designated class is by default not made visible outside of its own assembly. Use public __gc class hey ; the public keyword to modify the access of the a __gc designated class. A __gc designated class can be destroyed manually using the delete keyword, but only if the __gc designated class has a user-defined destructor. *An interface can be declared with the __gc extension keyword preceding it. Such as: //interface.cpp #using __gc __interface ClassBase The preceding code must be compiled with /clr and /LD to produce a simple DLL file. Notes: *A __gc __interface cannot contain any data members, static members, nested class declarations and no access specifiers. *A __gc __interface can only inherit from another __gc __interface interface or the System::Object. Inheritance from System::Object is the default behavior. *A __gc __interface cannot contain any implementation (body code) of its declared function prototypes.


Comparison with other languages

The following contains main points and programmatic standards that differ between Managed C++ and other well known programming languages that are similar in concept.


Standard C++

Disadvantages *
native Native may refer to: People * Jus soli, citizenship by right of birth * Indigenous peoples, peoples with a set of specific rights based on their historical ties to a particular territory ** Native Americans (disambiguation) In arts and entert ...
C++ code may be faster at runtime. *C++ does not require an installation of an associated compiler and managed runtime environment on the target system *C++ supports
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
. Until the final release of C++/CLI however, Managed C++ programmers must revert for workarounds for using generics. *C++ supports the keyword "const" and
const correctness In some programming languages, const is a type qualifier (a keyword applied to a data type) that indicates that the data is read-only. While this can be used to declare constants, in the C family of languages differs from similar constructs i ...
. Managed C++, like Java and C#, does not contain this feature. An alternative is to make a managed class immutable, or restricting set accessors on public interfaces. *C++ code is not constricted by the CLR's restrictions. For example, the CLR does not allow classes to inherit other classes privately nor protected, thus the following will produce a compiler error: public __gc class one ; public __gc class two: private one ; // error public __gc class three: protected one ; // error *Managed C++ __gc classes cannot inherit from more than one class, as such the following will produce a compiler error: __gc class a ; __gc class b ; __gc class c: public a, public b ; //will produce an error Advantages *Managed C++ supports a greater degree of
reflection Reflection or reflexion may refer to: Science and technology * Reflection (physics), a common wave phenomenon ** Specular reflection, reflection from a smooth surface *** Mirror image, a reflection in a mirror or in water ** Signal reflection, in ...
than regular C++, which is generally much more convenient depending on the function of the code, or what the code is intended for. *Managed C++ can inter-operate with all other .NET capable languages, including other third party languages. *Managed C++ is garbage collected. In standard C++, memory management and allocation is the responsibility of the programmer.


Java

Differences *Running Java code requires an appropriate virtual machine, while running Managed C++ code requires an appropriate implementation of the .NET Framework. Disadvantages *Java provides a documentation on the source code, while Managed C++ does not. *Java has many other development tools available for Java programmers to use, while Managed C++ is only available under Visual Studio .NET. Advantages *Managed C++ can access the computer system on a low level interface much more easily than Java. Java programmers must use the JNI (Java Native Interface) to use low level services of the host operating system.


C#

Differences *While C# supports pointers just as in C++, this feature is turned off by default. Disadvantages *Like
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
, C# is syntactically simpler when dealing with managed code. *C# can achieve basically the same result as Managed C++, as all syntactic and structural conventions remain strikingly similar. *Managed C++, though it is a strongly typed language due to its introduction into the CLR, can be prone to errors if unmanaged compiled code is introduced in the same codebase, while C# is pure MSIL. Advantages *C# must use the .NET Framework and provided class libraries to access the computer system on a low level. *Porting over applications to the .NET Framework from C or C++ is much easier to do using Managed C++. *The Microsoft Visual C++ .NET compiler, which compiles Managed C++ to target the .NET Framework, produces a much more matured set of instructions in its resultant assembly, thus improving performance.


See also

*
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...


References


External links


Managed Extensions for C++ (at MSDN)Article: C++ / CLI - How To Use Managed C++ DLL when Microsoft Visual C++ Redistributable is not installed?
{{Common Language Infrastructure C++ programming language family .NET programming languages Microsoft Visual Studio