Using c# code from a native c++ application - c#

I am working on a basic 2D C++ Game engine but i want to be able to use C# to make games with it like unity does but I'm not sure how to do it. I've seen people saying about CLI but not sure exactly how that works. I want to be able to access functions on the engine in C# and be able to have the engine run the C# code.
Thanks for any help with this.

Right now you have two big ways of doing this:
Make your C# application generate COM objects which you can consume from C++. Performance is a bit iffy, but doable and very simple.
Use reverse PInvoke, where you export functions from your C++ application with in function pointers that you fill in from the C# side with delegates to the functions that drive your code.
In .Net 5, there's a third way: you can directly export C# functions from your assemblies to be consumed in a platform-independent way, like in C++ (ie, .dll or .so exports).

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.

Using C# library in Unreal Engine C++

Hi I wish to use a C# library in a C++ application which is in unreal engine.
Is there any easy way to wrap a C# library using C++ ??
I have looked up how to wrap a C# library but there only seems to be posts on how to use a C++ library in C#.
Really appreciate the help thanks.
C# allows you to perform interoperability calls to c++ and c libraries. Although not exposed, you could manipulate your assembly to force the reverse (this is done via ildasm, however, this is a trick and I won't go in details).
To achieve what you ask, you have to manually fill in the gap from your c++ host and your c# library. One way, not clearly at zero effort, is to create a new assembly in MC++ (ie c++ with a hosted clr) . This bridging assembly would expose the api in order to be used by your c++ language, while it can access your c# library to reuse the logic.
This is not a trivial task as you need to manually marshal the data structures as you are going "against the designed use case".
Embedding a runtime, in my opinion, would have the same effort. In any case, you should be careful though, because your unreal engine is resource intensive and performances could get a sensible hit. Furthermore, the c# part has a different memory management system that can not be optimal when stressed by the unreal engine.

How can I add a C# GUI to C++ code?

I wrote C++ code in which are included some libraries and defined some functions plus the main. I am trying to add a GUI to this code in C#.
This code sends data (double[]) to a server. What I would make is create a graphic user interface using C#, that lets me to start sending data, clicking a button.
How could I make this?
I've tried to run the file .exe of the c++ project solution, but that doesn't work.
You have a lot of ways to run C++ code from C#:
COM
This is the canonical, good-bear way to do things, but I have a feeling this will be daunting at your level. Basically, you will expose your C++ classes as COM objects from your C++ project (dealing with converting between COM datatypes like _bstr_t for strings, et al). Then add this COM object as a reference in your C# project. This imposes the least pain on your C# code.
I suspect the biggest hurdle here is that you will need to understand how COM works, which is going to be pretty daunting if you don't already have experience in it.
C-like DLL
This is probably much easier, if you don't need to call many functions. It's the same as how you call native winapi functions from C#. You export C-style functions from your C++ DLL, and import them in C#. Converting between datatypes is done via the marshalling system in C#. So in general this places the burden more on the C# code, less on the C++ code.
The catch here is that you can't export much C++ stuff. So for example you cannot export a function like:
void myFunction(std::string& s);
Because std::string is a template, and will cause havoc regarding memory management and will probably just lead to heap corruption.
Rewrite in C#
If all you're doing is sending some data over a socket, then just write it in C#. For many things, C# is way faster than C++ for development, so it's probably worth looking into how much effort this would be.

C++ program hosting .net exposing objects from C++ to C#

i'm exploring the idea of use .net as a scripting language for my program.
i found a lot of examples on internet that shows how to host .net and then, from c++ call functions from .net.
But i need to go on the other way too, from this code in c#, i need to be able to create/call c++'s objects from this c# script, so i need to expose functions/objects to control it from C#.
How can i do it?
Simple Example to show what i'm talking about
I call my c++ method "CreateGUI". It'll call .net code: "InitializeGUI", and this "InitializeGUI" need to check if an object (for example, the texture) is instantiated inside C++.
Can someone help me?
Thanks!
If you use managed C++, you can create a managed C++ component which can be called from your C# code. If you use unmanaged C++, you can either create a C++ COM component and use it through C# via RCW (Runtime Callable Wrapper) mechanism or you can create a C++ dll and call it from C# via PInvoke.
You might also investigate Mono, which is explicitly intended to be embedded and will run any compiled .NET assembly, meaning the full powers of C#, VB.NET, even F# will be available.

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

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.

Categories

Resources