Foreword

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.

prerequisites

Visual Studio 2019 16.4 preview 3

.NET Core SDK 3.1 preview 2

Opening method

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)

First project

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:

How_to_mix_dotNET_Core_3.1_and_C++_program_0.png

Run the program, enter hello world and press Enter:

How_to_mix_dotNET_Core_3.1_and_C++_program_1.png

We can see that we successfully ran the program and completed the seamless interaction of C++ code with .NET Core.

be careful

  1. Managed heap objects are created using gcnew instead of new
  2. 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#.
  3. Use :: instead. Access namespace and class/struct, use -> instead. Access members of the object.
  4. Use the ref class/ref struct to define a .NET reference type and the value class/value struct to define a .NET value type.
  5. Define the interface using the interface class/interface struct.
  6. Use properties to define properties.
  7. 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

How_to_mix_dotNET_Core_3.1_and_C++_program_2.png

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.

How_to_mix_dotNET_Core_3.1_and_C++_program_3.png

Then we build a json string and try to serialize it with Newtonsoft.Json.

How_to_mix_dotNET_Core_3.1_and_C++_program_4.png

Run, output:

postscript

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):


Https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=vs-2019

references

Announcing .NET Core 3.1 Preview 2


An Update on C++/CLI and .NET Core



Https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=vs-2019

Orignal link:https://www.cnblogs.com/hez2010/p/11810683.html