How to mix dotNET Core 3.1 and C++ program
With the release of the second preview version of .NET Core 3.1, Microsoft officially ported C++/CLI to .NET Core, from which you can write .NET Core programs in C++.
Since only MSVC support currently compiles such mixed code, and because of the unmanaged code involved, C++/CLI is currently not cross-platform and only supports Windows.
If you need cross-platform, in addition to Microsoft’s work, you also need gcc/clang to follow up a lot, the workload is large and the progress is uncontrollable. At present, Microsoft has no plan to make C++/CLI cross-platform.
Visual Studio 2019 16.4 preview 3
.NET Core SDK 3.1 preview 2
Run the Visual Studio Installer, install the “Developer Development with C++” and “.NET Core Cross-Platform Development” workloads, and then tick “C++/CLI support for the v142 build tool (14.24)” in a single component.
Wait for the installation to complete, start Visual Studio, you can see two new project templates when you create a new project:
- CLR Class Library (.NET Core)
- CLR Empty Project (.NET Core)
We chose CLR Empty Project (.NET Core) to create our first C++/CLI project, then right-click on the source file in the right Solution Manager to add the C++ source file main.cpp.
Then we can write the .NET Core program in C++.
Add the following code:
Run the program, enter hello world and press Enter:
We can see that we successfully ran the program and completed the seamless interaction of C++ code with .NET Core.
- Managed heap objects are created using gcnew instead of new
- The type of managed heap object pointer is T^, not T*. Take the above code as an example. The type of str_managed is System::String^. Thanks to the automatic derivation of types that C++ 11 started with, we can use auto instead of display type declarations, similar to var in C#.
- Use :: instead. Access namespace and class/struct, use -> instead. Access members of the object.
- Use the ref class/ref struct to define a .NET reference type and the value class/value struct to define a .NET value type.
- Define the interface using the interface class/interface struct.
- Use properties to define properties.
- A C++/CLI project can reference any C++ project or dynamic link library, but make sure the architecture is the same, ie you can’t reference unmanaged code under the x64 architecture with x86 configuration.
Add project references and use
For reference to C++ code, I won’t go into details here, and there is no difference between the way it is used and the normal C++ project. So just talk about how to reference a .NET assembly here.
We can add a reference to the .NET Standard/.NET Core library directly. If you are unable to install using the nuget package manager, you can manually download the corresponding .NET assembly dll and add it to the project reference. Take Newtonsoft.Json as an example here.
First add a reference
Then we can use it!
Then we write a .NET type. To make it more complete, we use the full property writing method instead of the automatic property.
Then we build a json string and try to serialize it with Newtonsoft.Json.
Although C++/CLI can’t be cross-platform for the time being, support for .NET Core greatly enriches the scope of .NET Core, which can be used to write high-performance C++ programs while enjoying C++ and .NET Core/.NET. The entire ecology of the Standard.
For writing Windows programs, C++/CLI is definitely worth a try. Regarding the cross-platform issue, maybe the later version of Microsoft will support it? You know, two years ago, C++/CLI’s response to running Microsoft on .NET Core was “no plan”, but now it’s a complete support.
For other places in C++/CLI, you can refer to Microsoft’s official documentation, which is very rich in content. (Because there are some machine translations in Chinese documents, it is recommended that friends who have the ability to read English documents directly to avoid errors caused by machine translation):