C++ backend with C# frontend?

时光毁灭记忆、已成空白 提交于 2021-02-06 10:43:32

问题


I have a project in which I'll have to process 100s if not 1000s of messages a second and process/plot this data on graphs accordingly (The user will search for a set of data in which the graph will be plotted in real time, not literally having to plot 1000s of values on a graph).

I'm having trouble understanding using dlls for having the bulk of the message processing in C++ but then handing the information into a C# interface. Can someone dumb it down for me here?

Also, as speed will be a priority I was wondering if accessing across 2 different layers of code will have more of a performance hit that programming the project in it's entirety in C# , or certainly C++ though I've read bad things about programming a GUI in C++ in which, this application must also look modern, clean, professional etc. so I was thinking C# would be the way forward (perhaps XAML, wPF)

Thanks for your time.


回答1:


The simplest way to interop between a C/C++ DLL and a .NET Assembly is through p/invoke. On the C/C++ side, create a DLL as you would any other. On the C# side you create a p/invoke declaration. For example, say your DLL is mydll.dll and it exports a method void Foo():

[DllImport("mydll.dll")]
extern static void Foo();

That's it. You simply call Foo like any other static class method. The hard part is getting data marshalled and that is a complicated subject. If you are writing the DLL you can probably go out of your way to make the export functions easily marshalled. For more on the topic of p/invoke marshalling see here: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx.

You will take a performance hit when using p/invoke. Every time a managed application makes an unmanaged method call, it takes a hit crossing the managed/unmanaged boundary and then back again. When you marshal data, a lot of copying goes on. The copying can be reduced if necessary by using 'unsafe' C# code (using pointers to access unmanaged memory directly).

What you should be aware of is that all .NET applications are chock full of p/invoke calls. No .NET application can avoid making Operating System calls and every OS call has to cross into the unmanaged world of the OS. WinForms and even WPF GUI applications make that journey many hundreds, even thousands of times a second.

If it were my task, I would first do it 100% in C#. I would then profile it and tweak performance as necessary.




回答2:


If speed is your priority, C++ might be the better choice. Try to make some estimations about how hard the calculation really is (1000 messages can be trivial to handle in C# if the calculation per message is easy, and they can be too hard for even the best optimized program). C++ might have some more advantages (regarding performance) over C# if your algorithms are complex, involving different classes etc.

You might want to take a look at this question for a performance comparison.

Separiting back-end and front-end is a good idea. If you get performance penalty from having one in C++ and C# depends on how much data conversion is actually necessary.

I don't think programming the GUI is a pain in general. MFC might be painful, Qt is not (IMHO).

Maybe this gives you some points to start with!




回答3:


Another possible way to go: sounds like this task is a prime target for parallelization. Build your app in such a way that it can split its workload on several CPU cores or even different machines. Then you can solve your performance problems (if there will be any) by throwing hardware at them.




回答4:


If you have C/C++ source, consider linking it into C++/CLI .NET Assembly. This kind of project allows you to mix unmanaged code and put managed interfaces on it. The result is a simple .NET Assembly which is trivial to use in C# or VB.NET projects.

There is built in marshaling of simple types, so that you can call functions from the managed C++ side into the unmanaged side.

The only thing you need to be aware of is that when you marshall a delegate into a function pointer, that it doesn't hold a reference, so if you need the C++ to hold managed callbacks, you need to arrange for a reference to be held. Other than that, most of the built-in conversions work as expected. Visual studio will even let you debug across the boundary (turn on unmanaged debugging).

If you have a .lib, you can use it in a C++/CLI project as long as it's linked to the C-Runtime dynamically.




回答5:


You should really prototype this in C# before you start screwing around with marshalling and unmarshalling data into unsafe structures so that you can invoke functions in a C++ DLL. C# is very often faster than you think it'll be. Prototyping is cheap.



来源:https://stackoverflow.com/questions/4058821/c-backend-with-c-sharp-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!