How to smoothly develop a C lib for C#/.NET? - c#

I am developing a complex library in C++ and i plan on having a C interface so others can load up the DLL and easily access the lib. I haven't tried writing code in C# that access C code. I did a quick google and found code that uses a lot of attributes.
What can I do to keep my interface simple enough to not cause a headache trying to keep .NET in sync with it? Is there some kind of header generation tool i may use? Do i only use simple POD structs? I'm unsure how i should handle types as they are passed around as pointers. I am also thinking maybe i should avoid using anything that is a not an int/string or array.
I am developing it using MSVC but mostly using it with GCC. I know i should use the calling convention __stdcall. Beyond what i said i am totally clueless. I actually dont know how to load the DLL into .NET.
What can i do to ensure everything works correctly when writing my C lib and getting it to run with .NET?

Consider putting together a COM interface. Consuming COM from .NET is marginally easier than P/Invoke; at least you won't have to spell out prototypes for all functions in C#, the COM typelib importer will do that for you.

Related

How to call C++/C# code from NodeJS/ElectronJS to interact with a DLL?

Use Case
I have a DLL written in C/C++ that is the .dll file and a .h. I need to write an ElectronJS app (.exe) with NodeJS as backend to access some function calls as part of a bigger program. I also have a C# program that has implemented a basic program that uses the DLL.
So Far
Im able to interact with some basic functions of the DLL with node-ffi-napi and ref-napi. The main problem here is that the dll uses some really big structs (>15members of short/long/DWORD/etc) and struct pointers to obtain information. While there is ref-struct-di it seems that it would be very hardwork to try to implement big structures and pointers to make an API wrapper on Node since is not native code.
Options
I think there is 3 options but since im not very familiar with Node i dont know which would be the best, what tools/packages to uses or if there is another way.
Hard work trying to implement my structs and structs* with ref-struct-di and a really extensive testing to check every parameter to see if the struct is well implemented and fail proof (seg faults) since is not a native tool.
Use the basic implemented C# tool that already does a wrapper to the dll API and have the functions implemented. Is there a way from my Node app to have some C# code running also? So i can call the C# wrapper that has native structs and see how to convert them to a dictionary so Node use them.
Implement a C++ wrapper to do similar to the C# program. This way i can access the dll api (and the .h) easier to handle the calls and structs/structs* and then from Node, call some C++ functions like for example GetInformation() and return a dictionary instead of the dll original funcion.
Since the ElectronJS app will be an .exe i need to handle everything on the same program. Its possible?
To sum everything up, what would be the best way to handle the DLL? Is there a way from Node to have some C++/C# code running also and interact with the app in runtime so i can call the dll? Anyone know any tool/package/github/link to use/read more about?
Thank you in advance.
EDIT:
Im reading about Node-API to integrate C++ code as addon. As my understanding, i will have a C++/.h files inside my Electron app and then compile everything. So my C++ code calls the DLL and then from Node i call the C++ wrapper. Is this correct?
Another way would be to create a new DLL in C++ that calls the original DLL. Then with ffi from Node access the new DLL that returns me a json/string or something less "complex" with only the info i need and not all the big structs. Right? Ideas? this way seems easier to write two separate programs without the N-API but would require two compilations.

Up to date solution to expose C# .net assemblies to C++

I have C++ Qt application together with a lot of .net C# assemblies, that I would need to use in the C++. Before doing any research I was expecting there is existing solution to generate C++/CLI wrapper DLL for usage in pure C++ app, but to my surprise I wasn't able to find anything except SharpCpp, but that's just experiment.
As the amount of classes that need to be exposed might be pretty extensive, I was looking for as much as automatic solution possible ( like what I'm used to when wrapping C++ for Python using shiboken)
As far as I can tell my possibilities are (as just compiling the app with /clr is not an option):
Manually write C++/CLI bridge DLLs, but this could take a lot of time.
use COM, but that would also mean a need to implement the COM exposure to all C# assemblies also. Or is there any automated solution?
use CLR Hosting. Not an option without automatic generation of the wrapper also.
try using something like Alter-Native or cs2cpp, but I don't know how much production ready this is.
So I was wondering, if someone was facing similar problem, and what turned out to be the best, and most maintainable solution.

Using a C++/CLI/CX/etc data package from an UWP C# app

After days of experimenting that only led to partial success, I'd like to ask whether I have any chance or I'll invariably end up in a dead end. I have an UWP C# App, the usual framework, planned to be distributed in the Windows Store. And I also have a data package written in C++ (mostly C) that I used earlier. The old, non-managed code doesn't call any Windows API at all, it's just a data format package. But I need to access it directly from the C# side, and its most important type is authored as a value struct, with many overloaded operators (and this is good so, that approach is just perfect for the application domain).
From a WPF application, I wouldn't have any problem at all, a C++/CLI wrapper of a value struct, exposing everything. But the UWP app doesn't want to do the same. If I use the same C++/CLI wrapper, although I can get it to compile by itself, the UWP project will flat out refuse to reference the C++/CLI project.
I also tried the newer C++/CX flavor but that comes with many limitations, no specialized constructors, no overloading. It seems to be sandboxed much more than I'd need.
Is there any solution I missed? Maybe still using the C++/CLI (which has the benefit of being already written :-) ) somehow from under an UWP application?
Starting with version 1803 you should have access to a complete C++ implementation
C++/WinRT is an entirely standard modern C++17 language projection
for Windows Runtime
If you want to consume C++ code from C#, then you probably want to compile it as a Windows Runtime Component, as those can be consumed in any other UWP-supported language.
I think this is demonstrated in this documentation, even through the app consuming it seems to be in C++
In the end, I succeeded. There is an intermediate helper class (not a struct because of the inherent limitations) written in C++/CX, and the actual struct I use is defined in C#, using this intermediate.
The error messages in the process were mostly related to the underlying code being very old, C, not even C++, with all the linking and externing stuff involved. But no matter how old it is, it still works...

C# Wrapper for C++

I'm working on an app that I would really like to write in C#, but I need to use a library that is in C++. The vendor supplements their DLL with some C++ code to make the API more convenient, which it does, but it's still C++. I'd like to incorporate this extra C++ code into my app. It seems reasonable to create a DLL to wrap the vendor-supplied C++ code and the other calls into a module that I can use in C#.
My question is, does it make sense to wrap a DLL in another DLL? Are the potential problems I should watch out for?
Best,
John
Wrapping a wrapper for the sake of creating a better API (or, in your case, a .NET API) is OK. You might face some interop problems, as they can always pop up when moving from managed to unmanaged code.

Marshal a C++ class to C#

I need to access code in a native C++ DLL in some C# code but am having issues figuring out the marshaling. I've done this before with code that was straight C, but seem to have found that it's not directly possible with C++ classes. Made even more complicated by the fact that many of the classes contain virtual or inline functions. I even tried passing the headers through the PInvoke Interop Assistant, but it would choke on just about everything and not really no what to do... I'm guessing because it's not really supported.
So how, if at all possible, can you use a native C++ class DLL from .NET code.
If I have to use some intermediary (CLR C++?) that's fine.
There is really no easy way to do this in C# - I'd recommend making a simple wrapper layer in C++/CLI that exposes the C++ classes as .NET classes.
It is possible to use P/Invoke and lots of cleverness to call C++ virtual methods, but I wouldn't go this route unless you really had no other choice.
You might have a look at SWIG, if nothing else just to see how they handle the basic wrapper code (which SWIG generates). I know next to nothing about C# native code bindings so I can't judge if their approach is sound, but they do a decent job with some of the other language bindings.

Categories

Resources