非托管到托管选项:性能注意事项
初步说明:调用者是一个本机 EXE,它公开了一种“插件”架构。 它旨在加载 DLL(按名称,指定为命令行参数)。 该 DLL 必须是本机的,并导出特定的函数签名。 EXE 是 C++,这并不是太重要,因为 EXE 是一个黑匣子(无法修改/重新编译)。 本机DLL可以通过在所述DLL中完全本机地实现解决方案来满足应用程序的需求。 然而,一个要求是允许实际工作(从而将本机 DLL 转变为瘦包装器/网关)用 C# 进行编码。 这给我带来了 3 个选项(如果有更多,请分享):
- Native DLL 加载内部使用 C# 类库的 C++/Cli DLL
- Native DLL 通过 CCW 与 C# COM 对象交互
- Native DLL 托管 CLR 并进行调用对 C# 程序集的
另一个要求是,本机 DLL 不仅需要一种在 C# 上发送消息(调用函数)的方法,而且当发生某些特殊情况时,C# 需要能够向本机 DLL 触发事件/回调(而不是关闭并返回)。 现在,我不确定如何在第三个选项中处理最后一件事,但这完全是另一个问题。
言归正传:性能。 有关这些方法的任何信息(假设它们都满足要求)? 根据我的调查,我的理解是 2 会比 1 有更多的开销,但我不是 100% 有信心,这就是我在这里的原因。 至于3,我还没有任何信息。
因此,如果有人处理过这些(或知道另一个优雅的选择),请插话。
谢谢!
Preliminary: The caller is a native EXE that exposes a type of "plugin" architecture. It is designed to load a DLL (by name, specified as a command line arg). That DLL must be native, and export a specific function signature. The EXE is C++, which isn't too important since the EXE is a black box (cannot be modified/recompiled). The native DLL can meet the application needs by completely implementing the solution natively, in said DLL. However, a requirement is to allow the real work (thus turning the native DLL into a thin wrapper/gateway) to be coded in C#. This leads me to 3 options (if there are more, please share):
- Native DLL loads a C++/Cli DLL that internally makes use of a C# class library
- Native DLL interacts with a C# COM object via CCW
- Native DLL hosts CLR and makes calls to C# assembly
One more requirement is that not only does the native DLL need a way to send messages (call functions) on the C#, but the C# needs to be able to fire events/callback to the native DLL when certain extraordinary things occur (as opposed to shutting down and returning). Now this last thing I'm not sure how to handle in the 3rd option, but that is another question altogether.
So to the point: performance. Any info regarding those approaches (assuming they all meet the requirements)? From my investigation, my understanding is 2 would have more overhead than 1, but I'm not 100% confident, which is why I'm here. As for 3, I just don't have any info yet.
So if anyone has dealt with these (or knows of another elegant option), please chime in.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我之前已经完成了选项 1,并取得了一定的成功。 我不记得有任何重大的性能影响,尽管我的应用程序并不是非常注重性能。 在我看来,如果出现性能问题,罪魁祸首可能是频繁的、小型的本机到托管转换。 是否可以在 C++/CLI 层对这些进行批处理?
I've done option 1 before, with reasonable success. I don't remember any significant performance implications, though my application wasn't terribly performance-intensive. It seems to me that if performance problems occur, a likely culprit might be the frequent, small native-to-managed transitions. Would it be possible to batch those at the C++/CLI layer?