April 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Interop: A Look at Managed C++

  • December 5, 2002
  • By Jason Clark
  • Send Email »
  • More Articles »

by Jason Clark of Wintellect

There are two forms of interop built into the .NET Framework Common Language Runtime. These are Platform Invoke and COM Interop. The purpose of these interop features is to allow your managed .NET code to interface with legacy unmanaged code. Platform Invoke, or P/Invoke, is a low-level means of calling into system functions, such as DLL entry point functions. COM Interop is built on top of P/Invoke, and manages higher-level details related to COM, such as COM object lifetime, method signature translation, etc. It is also possible for language compilers to offer their own interop features built on top of P/Invoke. Microsoft's Managed C++ compiler does this.

IL and C++

We will look at Managed C++ or MC++ more in a bit, but first let's talk about managed code in general. Managed code is code that runs on the Common Language Runtime (CLR). Managed code is a combination of Intermediate Language and Metadata. Metadata describes managed code in detail, so that runtime features such as garbage collection and type safety are enforceable by the system. Intermediate Language or IL is a .NET p-code. IL makes up the logic of managed methods, and is Just-in-Time (JIT) compiled into native machine language when the code is executed. IL can access managed primitive types such as System.Int32, as well as more high-level class library types such as System.Form.

Even though IL is managed, by nature, it does include instructions for accessing memory directly through pointers. This ability, in conjunction with P/Invoke features of the CLR, is what makes MC++ possible.

Managed C++ is regular C++, but with the ability to access features of the Common Language Runtime. In brief, the MC++ compiler lets you link-together modules that are compiled into x86 machine language, with modules that are compiled down to IL and metadata. In fact, using a #pragma managed, and #pragma unmanaged, you can coerce the compiler into producing both x86 and IL from functions defined in the same source code module. The only required step is to include the /CLR switch when building your module, and MC++ will produce IL from your C++ logic!

This ability to build C++ logic down to IL and metadata is the foundation of the features of managed C++, but it alone doesn't really affect much. All of the memory allocation and API calls are still unmanaged, they are just happening through IL, that's all. However, once you are compiling to IL, rather than directly to machine language, it is a small leap to begin accessing features of the .NET Framework. This is where the benefits begin to show.

In keeping with the Microsoft tradition of extending C++, MC++ extends the language with a number of extended keywords that start with two underbars, such as __gc or __box. These extended keywords are called Managed Extensions, and they provide access to features of managed code, such as garbage collection and use of managed types.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel