An Update on C++/CLI and .NET Core
The first public release of our C++/CLI support for .NET Core 3.1 is now available for public preview! It is included in Visual Studio 2019 update 16.4 Preview 2. We would love it if you could try it out and send us any feedback you have. For more info about what this is and the roadmap going forward, check out my last post on the future of C++/CLI and .NET Core.
To get started make sure you have all the necessary components installed. C++/CLI support for desktop development is an optional component, so you will need to select it on the installer’s right pane:
You will also need the .NET Core cross-platform development workload. It installs everything you need including the .NET Core 3.1 SDK:
Creating a C++/CLI .NET Core Project
First, you will want to create a “CLR Class Library (.NET Core)” or “CLR Empty Project (.NET Code)”. The Class library template includes some additional boiler plate to set up an example class and precompiled header that may make it easier to get started. The empty project is ideal for bringing in existing C++/CLI code. Retargeting existing C++/CLI projects to .NET Core isn’t recommended.
There isn’t currently a template for C++/CLI console or Windows applications that can be used with .NET Core. Instead you must put application entry point outside of the C++/CLI code. In general, we strongly recommend keeping the C++/CLI projects as narrow in scope as possible to handle just the interoperability between .NET Core and other C++ code.
Once you create one of these projects, you can reference it from other .NET Core projects like any other class library – with one important caveat. .NET Core projects are typically architecture agnostic. You see this as the architecture “Any CPU” in the Configuration Manager and “MSIL” in the build logs. This is the default for all .NET Core projects. If you reference any C++/CLI class libraries, you must specify an explicit architecture for the non-C++ projects instead of “Any CPU”.
You can set a project’s architecture by using the Configuration Manager.
If the architectures don’t match, you will see this warning and attempting to load the C++/CLI class library will fail at runtime:
To resolve this, make sure all projects in the solution are using the same architecture of “x86” and “Win32” or “x64”. If you are using ASP.NET Core, there is an additional consideration. Your projects also need to match the architecture of IIS Express. This is typically “x64”. If you see a “500 server error” due to the loader failing, this may be what the problem is.
Send us Feedback
Please try this out. We’d love to hear your feedback to help us prioritize and build the right features. We can be reached via the comments below or email (email@example.com). You also can always send us general feedback via Developer Community.
Isn’t C++/CLI dead? It’s not been a vibrant project recently.
Not quite. It’s used in a LOT of code which are layers between native c++ legacy or high-performance code and, say, C# UI. I look forward to it being supported with .NET Core for exactly this need. If .NET Framework is essentially dead (no new features), our C# could be migrated to .NET Core WPF and still interop via C++/CLI with an existing backend which is a *large* body of C++ code.
A whole lot of applications use them. Thanks for adding support! I have worked on a ~$200M Class II medical device product that has a TON of C++/CLI to access legacy code and it also works great for near real-time applications (until you stumble upon / inject a memory leak and then your life is a misery).
Yep, the whole purpose of C++/CLI at this point in time is to put a .NET interface on existing C++ libraries.
Awesome will try it immediately. This may be a bit orthogonal but does this release make it easier to use .NET nuget packages in a C++/CLI solution? In an ideal world this would mean being able to use PackageReference in the project file.
The main changes in the project file I saw were
<Keyword>ManagedCProj</Keyword> -> <Keyword>NetCoreCProj</Keyword>
<TargetFramework>v4.7</TargetFramework> -> <TargetFramework>netcoreapp3.1</TargetFramework>
<CLRSupport>true</CLRSupport> -> <CLRSupport>NetCore/CLRSupport>
Is that it or are there others I missed?
This is a pretty big deal for me. If I’m going to write in .net core I will want to use nuget references. Is this coming? Is there at least be a reasonable workaround?
I have been trying it to test a project where we have a CLR Class Library project that outputs a dll and calls some functions developed in an unmanaged C++ project (static library) that calls some functions of the CGAL library. I have tested the calls of the CLR Class library (named CGALWrapper) from a new demo Console App C# .Net Core project. Everything goes fine until the code reaches the section where the call to the CLR Class library dll is made where I get
System.BadImageFormatException: ‘Could not load file or assembly ‘CGALWrapper, Version=1.0.7248.23049, Culture=neutral, PublicKeyToken=null An attempt was made to load a program with an incorrect format.’
I am using:
*Microsoft Visual Studio Community 2019 Preview Version 16.4.0 Preview 3.0
For the CLR CLass library project I have created a new CLR Class library (.NET Core) project as commented on this post and also a new .Net Core Console App C# for the project that tests the calls. Only the unmanaged C++ project has not been modified. Everything has been compiled for Debug-x64 (I have removed any other configurations to avoid any possible problems).
What I would like to ask is if the new C++/CLI support for .NET Core 3.1 supports C++/CLI projects that include code from unmanaged C++ projects
hey will buik! alles goed/
Does anyone have advise on how to get the code package downloaded on visual studios professional 2017?
AFAIK, support for c++ /CLI is only in VS2019 16.4 (still in preview).
Is there a particular reason why this couldn’t be cross platform? Without the UI piece it isn’t as critical but if .NET does get a completely cross platform UI I would hate to figure out how to use PInvoke (assume that can be used?) to call a large C++ calculation library.
Presumably they still use their own C++ compiler to generate code, and the MS C++ compiler isn’t cross-platform.
If we build a simple C++/CLI project (NET Core 3.1) referencing a C# project (Net Standard 2.0/2.1) then we get warnings like:
>source\repos\ClassLibrary8\CLR\CLR.cpp(9,56): warning C4691: ‘System::Object’: type referenced was expected in unreferenced assembly ‘netstandard’, type defined in current translation unit used instead
>source\repos\ClassLibrary8\CLR\CLR.cpp(9,56): message : This diagnostic occurred while importing type ‘ClassLibrary8::Class1 ‘ from assembly ‘ClassLibrary8, Version=126.96.36.199, Culture=neutral, PublicKeyToken=null’.
Changing the C# project to .Net Core 3.1 makes the code compile cleanly, but we’d prefer to keep most of our projects Net Standard 2.x
At some point, don’t you need to choose your underlying runtime (be it .Net “standard” or .Net “Core”)?
What kind of scenario do you see here?
Hi VC team,
Thank you very much for this great and long awaited feature!
This has been a long time in the making but it finally arrived!
We (native devs) now have a way to support the .Net “Core” runtime :-)!
I see great vcprojx support in both compiler _and_ IDE. Thanks!
How about CMake support? Is it too much to ask for?
Any guidance from Microsoft on how to have the best of both worlds (CMake & /clr:netcore)?
Thank you anyway! Awesome work on this!
I have a c++ /clr project that use winForm and these libs:
legacy_stdio_definitions.lib, Ws2_32.lib, Kernel32.lib, Iphlpapi.lib, User32.lib, Synchronization.lib, .., Winmm.lib, Userenv.lib.
My big question is .. do you think that I can Port it to .NET Core ?
Can we use donet publish for C++ CLI project?