How to add CLI to C# WPF project? - c#

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://learn.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.

Related

Cross platform C# and C++ VS2022 solution

I'm currently converting a VS solution of a C# web application targeting .NET Framework 4.8, that has interop to C++ native libraries using C++/CLI as a bridge.
My goal is to port it to .NET 6, and make it all cross platform, so C++/CLI has to go. Also I need to make changes to my C++ native library projects which currently have dependencies to Windows SDK.
I've read that CMake is the way to go, and that VS 2022 has good support for it. I'm trying to set up a sample solution that has a single C# project and a CMake project, but it has proven to be very challenging.
It's like there is no such thing as a CMake project, but rather just a folder with a special CMakeLists.txt file.
When I add a CMake project it unloads the VS solution file, and the whole IDE changes to the Opened Folder mode. I cannot add an existing CMake project to a solution, because there is no project file.
The experience is sub-optimal.
The VS solution I'm trying to convert has a handful of C# and C++ projects, and it would be nice to be able to keep them all in the same solution.
Am I doing something fundamentally wrong? Should I be using something else instead of CMake?

Using a C# class from a managed C++ project

I have a solution with a managed C++ project and a C# project. The C# project is a class library project containing the GUI classes I use from the C++ project. This works well, but building results in two DLLs.
Is there a way of using the C# objects from the C++ project without having a dynamically linked library generated by the C# project (is there a way to embed the C# types into the managed C++ project)?
I am using Visual Studio 2015.
There is a way, but it is far outside the tooling you are using to be able to accomplish it.
You can use command line tooling to create a multi netmodule assembly.
Managed assemblies are built of one or more "netmodules". Typically in a C# project, there is only one netmodule, and the C# compiler builds it and then links it into the assembly it produces. By using the command line tools, you can build only a module, then link it with the C++ module into an assembly.

Native C++ dll in one solution with C# project

I have a source file written in C++, performing some simulation. I would like to create a user interface for this in C#. How can I have both C++ and C# in a single Visual Studio solution?
What I did so far was I created a .NET C# project and solution, and then a native project as a DLL library. I have marked the C# project as "Depends on" the C++ dll. I have provided the necessary dllexport directives and imported using DllImport. Everything compiles fine, but when I execute the application, the dll is not found:
Unable to load DLL 'xxxx.dll': The specified module could not be found.
It is no surprise the DLL is not found, as the application is run from its output directory and the output directories are different for the C# and C++ projects. I do not want to write an explicit path as my import like `"..\..\Debug\xxxxx.dll".
Is there some common way how to structure a solution containing native a Dll and C# app so that the C# app can call the native Dll?
If you know that after deployment your C++ DLL will be in the same folder as your C# DLL (or executable), then on simple way to solve the problem is to use the PostBuild event (Project properties, Build events).
You can add a copy command that will put your C++ DLL into the C# output folder.
I found a very comfortable way, which might have some unexpected drawbacks, but I do not see any so far: I can enable CLR for the C++ DLL project, making it "mixed", even if it in fact does not contain any managed code, and then the project building it can be made a Reference in the C# .NET project.
To make the project mixed mode set Configuration Properties / General / Common Language Runtime Support to Common Language Runtime Support (/clr).
After this the build system copies the DLL into the application output folder automatically.

Trying to link C++ library with C# using swig - will not let me add reference to resulging dll to C# project

I'm trying to make c# bindings for a library. I used cmake to create a visual studio 2010 solution from the library source, and that compiled fine. I used the included .i file to create the wrappers as per the instructions here (https://code.google.com/p/labstreaminglayer/source/browse/LSL/liblsl-Generic/AUTOGENERATE%20HOWTO.txt ) using cygwin. I then added the resulting liblsl_warp.cxx file to the same project. It still compiled fine, so I would assume that the resulting .dll includes the required bindings.
I've added that project to my c# application and am trying to add a reference from the c# project to the resulting dll (also tried adding a reference to the project). The only error I get is A reference to 'lsl' could not be added..
What steps am I missing? I looked at a couple examples, and I am basically setup the same way, but their examples are significantly easier.
SWIG produces both C# and C++ source code. The C# code uses DllImport to import the functions from the C++ side - it doesn't use .net-like bindings. Therefore the C++ DLL does not need to be (and cannot be) registered, and hence can't be 'referenced'. Referencing is for registered DLLs.
So you have a C++ DLL and a C# exe. Just ensure the C++ DLL can be found by the exe, by putting it in the same folder for example. Don't worry about referencing it.

Auto wrap c++ dll into c#

I want to use c++ library in a c# project. Is there any wrapper tool to import all classes automatically?
SWIG can help create a wrapper consisting of two parts, one C++ sided, and one C# sided.
It needs a bit of work to set up the correct generation files though.
An alternative that requires more manual coding is C++/CLI.
For pure c apis I prefer p/invoke over either of them. There is a program to automate conversion of c headers. If I recall correctly it's called something like "P/Invoke Interop Assistant" or "Interop Signature Toolkit".
There is also mono/cxxi which looks pretty cool.
The procedure of using native .dll's in .Net is called P/Invoke. Look at http://pinvoke.net/ for some examples.
Note that you must match the build target with the version of the .dll. So for x86 .dll's you need to lock your project to x86, same with x64.
Note2 that you only need to lock the executing project (.EXE), not any additional projects loaded from the .EXE. .Net will automatically match .Net .dll's to CPU target type if they are set to ANY.
From http://social.msdn.microsoft.com/forums/en-US/clr/thread/c957959e-0f0c-422e-a5be-4ccfdd12e63d: You can use "dumpbin /exports <name_of_your_dll>" or dependency walker (depends.exe) to look at the exported symbols. They are both included in Visual Studio.
Additional comment on C++: While it is relatively simple to use native .dll's written in C from .Net, using C++ binaries that make use of objects is not as trivial. One way to solve that is to use a C++ CLI project a binding between managed .Net code and unmanaged C++ library.
If this is unmanaged code then you could use P/Invoke. Another possibility is to use the C++/CLI extensions to compile the code into a managed assembly that you could directly use.

Categories

Resources