C++ 后端与 C# 前端? [英] C++ backend with C# frontend?

查看:124
本文介绍了C++ 后端与 C# 前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,如果不是每秒 1000 条消息,我将不得不处理 100 条消息,并相应地在图表上处理/绘制这些数据(用户将搜索一组数据,其中将绘制图表实时,无需在图表上绘制 1000 个值).

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).

我无法理解使用 dll 在 C++ 中处理大量消息,然后将信息传递到 C# 接口.有人可以在这里为我哑口无言吗?

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?

此外,由于速度将是一个优先事项,我想知道跨 2 个不同的代码层访问是否会对整个项目的性能造成更大的影响,即使用 C# 或 C++ 对整个项目进行编程,尽管我已经阅读了一些关于用 C++ 编写 GUI,其中该应用程序还必须看起来现代、干净、专业等.所以我认为 C# 将是前进的方向(也许 XAML、wPF)

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)

感谢您的时间.

推荐答案

在 C/C++ DLL 和 .NET 程序集之间进行互操作的最简单方法是通过 p/invoke.在 C/C++ 方面,创建一个 DLL,就像您创建任何其他 DLL 一样.在 C# 端,您创建一个 p/invoke 声明.例如,假设您的 DLL 是 mydll.dll 并且它导出一个方法 void Foo():

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();

就是这样.您只需像任何其他静态类方法一样调用 Foo 即可.困难的部分是整理数据,这是一个复杂的主题.如果您正在编写 DLL,您可能会不遗余力地使导出函数易于编组.有关 p/invoke 编组主题的更多信息,请参见此处:http://msdn.microsoft.com/en-us/magazine/cc164123.aspx.

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.

在使用 p/invoke 时,您会受到性能影响.每次托管应用程序进行非托管方法调用时,它都会跨越托管/非托管边界,然后再次返回.当您封送数据时,会进行大量复制.如有必要,可以通过使用不安全"C# 代码(使用指针直接访问非托管内存)来减少复制.

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).

您应该注意的是,所有 .NET 应用程序都充满了 p/invoke 调用.没有任何 .NET 应用程序可以避免进行操作系统调用,并且每个操作系统调用都必须进入操作系统的非托管世界.WinForms 甚至 WPF GUI 应用程序使这一过程每秒进行数百次甚至数千次.

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.

如果这是我的任务,我会首先 100% 用 C# 完成.然后我会对其进行分析并根据需要调整性能.

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

这篇关于C++ 后端与 C# 前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆