简体   繁体   中英

How to add CLI to C# WPF project?

I have c# WPF project and now I need to add c++ support. I know that it is possible by CLI implementation like a bridge.

I found this tutorial https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=vs-2019

But there is no words about how to integrate CLI to my C# project.

I thought that I need to create .h + .cpp files like CLI, but when I click Add -> New Item there is no C++ choice

How to start with it?

I used to create C++/CLI for UWP projects and pretty sure WPF does almost the same way.

First of all, run Visual Studio Installer, go to "Desktop C++ Development" and make sure you have everything related to C++/CLI at "installed" state (because C++/CLI is usually not included into default set).

Then add a new project (as C++/CLI gate can not be a part of the same WPF or UWP project), you could find appropriate class lib in the templates tree under "C++ -> CLR"

Then you should reference your C++ staff (DLL, whatever) at the created C++/CLI project, and reference the C++/CLI project in the main WPF project.

You can try this as a starting point in case you stuck: https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper

I have a good example of how to use native C++ components in managed applications: NativeWPF .

The preferred way to do this is to add a new assembly with CLI support to the solution. In this project, you should write all your unsafe C++ classes, as well as C++/CLI code, which is the link between managed C# code and unmanaged C++. After that, you should add a reference to this project in your WPF application.

C++/CLI has a slightly different syntax, I recommend referring to the official Microsoft documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM