Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News C++ is Coming to .NET Core for Windows

C++ is Coming to .NET Core for Windows

This item in japanese


Microsoft has announced plans to offer C++/CLI in .NET Core 3.1. This would only be offered for Windows applications; you won’t be able to use C++/CLI for Linux or OSX.

C++/CLI is Microsoft’s second attempt at creating a version of C++ that can target .NET. Created in 2005, it replaced Managed Extensions for C++. The purpose is to allow developers to freely mix managed (i.e. .NET) objects with traditional C++ objects.

The first use case that comes to mind is interoperability. While VB and C# can directly call native DLLs and COM components, those libraries don’t always offer an appropriate API. By wrapping them in a C++/CLI library, abstractions can be put into place that hide the incompatibilities.

Another use is direct hardware access. When working with hardware, developers often need to carefully manipulate unmanaged memory. C# allows this in “unsafe” blocks, but only to a limited degree. C++, on the other hand, can use inline assembly to get around any language-based limitations.

There are restrictions here. Hans Passant explains,

The C++/CLI compiler supports generating both machine code and IL. It will generate machine code for any code that is compiled without /clr in effect or functions that are bracketed with #pragma managed(push, off) and #pragma managed(pop). Or functions that must be compiled to machine code because they contain code that cannot be translated to IL. Like _asm, note the C4793 warning you got for the first snippet. Suppress the warning with #pragma managed. Such code cannot use any managed types of course.

The unit of code generation is a function. What cannot work is a function that needs both. Your main() function must be compiled to IL because it uses managed types. The function won't be inlined of course.

Presumably because the Microsoft Visual C++ compiler only targets Windows, C++/CLI likewise can only target Windows. This mixing of native and IL code would require a platform-specific compiler for each operating system.

This is not an impossible goal. C++/CLI has been standardized as ECMA-372. So in theory LLVM or gcc could adopt the extension and offer a Linux or OSX version.

Other C++/.NET Languages

While C++/CLI is still a viable option, it wasn’t Microsoft’s last attempt. As part of Windows 8 and the WinRT initiative, Microsoft created C++/CX. This resembles the C++/CLI syntax, but was designed specifically to work with WinRT’s version of COM.

In 2015, C++/CX was replaced with C++/WinRT. Unlike the other languages discussed in this article, C++/WinRT isn’t an extension to C++. Based on C++ 11 and 14, developers can now write ‘normal’ C++ code while still accessing the WinRT/COM infrastructure. For more information, see CppCon 2016: Kenny Kerr & James McNellis “Embracing Standard C++ for the Windows Runtime.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p