C++/C# 回调继续

发布于 2024-07-23 12:20:02 字数 578 浏览 10 评论 0原文

在提出这个问题之后显然这让人们很困惑,想一想如何——我可以将一个缓冲区从 C# 应用程序提供给 C++ dll,然后在 C# 中创建一个计时事件,将缓冲区的内容复制出来吗? 这样,我就可以避免显然发生的回调调用造成的任何延迟。 这会起作用吗?或者编组会阻止这种缓冲区访问吗? 或者我是否必须进入不安全模式,如果我这样做或不这样做,让它发挥作用的神奇词是什么?

回顾一下另一个问题:

  • 我有一个用 C++ 编写的驱动程序和一个用 C# 编写的应用程序。
  • 我需要以预览方式从驱动程序获取数据。
  • C++ 应用程序与 C++ dll 交互效果很好; C# 应用程序复制数据的延迟很大。
  • 延迟似乎不是由 C# 端的发布/调试差异引起的,
  • 我需要解决延迟问题。 这个提议的缓冲方案能起作用吗? C# 应用程序是否可以使用 C++ dll 写入的缓冲区,或者我需要执行其他操作吗?

After asking this question and apparently stumping people, how's about this for a thought-- could I give a buffer from a C# application to a C++ dll, and then have a timing event in C# just copy the contents of the buffer out? That way, I avoid any delays caused by callback calling that apparently happen. Would that work, or does marshalling prevent that kind of buffer access? Or would I have to go to unsafe mode, and if I do or don't, what would be the magic words to make it work?

To recap from that other question:

  • I have a driver written in C++ and an app written in C#.
  • I need to get data from the driver in a preview-style manner.
  • C++ applications interact with the C++ dll just fine; the C# app has a large delay for copying the data over.
  • The delay does not appear to be caused by release/debug differences on the C# side
  • I need to get around the delay. Could this proposed buffer scheme work? Could a C# app consume from a buffer written to by a C++ dll, or do I need to do something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尤怨 2024-07-30 12:20:03

是的,您可以将缓冲区(例如 byte[])从 C# 传递到 C++。 我被你原来的问题难住了。 我使用从我自己的 C++ 代码到 C# 的回调,并且我从未注意到性能问题——尽管我尝试不在循环的每次迭代上执行回调,因为我确实期望一些封送开销。

Yes, you can pass a buffer (eg. byte[]) from C# to C++. I am stumped by your original problem. I use callbacks from my own C++ code to C# and I have never noticed a performance problem--although I try not to do callbacks on every iteration of a loop, as I do expect some marshaling overhead.

冷月断魂刀 2024-07-30 12:20:03

正如我在另一个线程中所说,延迟似乎完全是在 C++ 方面,所以我这边再多的欺骗也无法解决问题。

As I said in the other thread, it seems the delay was entirely on the C++ side, so no amount of finagling on my end was going to fix the problem.

痴梦一场 2024-07-30 12:20:02

如果您希望

  1. 在托管代码中使用非托管代码中的数据
    那么你就会遇到问题,除非你将它作为字节指针(不安全代码)使用或者你获取一个副本(封送处理将为你完成)。 后者很可能会减慢您的速度(但不要猜测基准)。

  2. 在非托管代码中使用托管代码中的数据
    那么这非常简单,您只需确保在非托管代码使用关联的缓冲区时固定该缓冲区即可。

听起来您希望这样做 1. 您是否对延迟进行了基准测试? 如果是复制(不感兴趣的缓冲区有多大),那么您可以更改 C# 代码以将缓冲区作为字节指针(和长度)进行交互吗?
如果您有权访问 C++/CLI,您通常可以更干净地解决这个问题,因为更容易让部分代码不受管理(使用原始缓冲区),而受管理的部分只使用它的子部分(仅复制绝对需要什么)

If you wish to

  1. Consume data from unmanaged code in managed code
    Then you have an issue unless you consume it as a byte pointer (unsafe code) or you take a copy (which marshaling will do fo you). It is likely the latter which is slowing you down (but don't guess benchmark).

  2. Consume data from managed code in unmanaged code
    Then this is pretty simple you just have to make sure you pin the associated buffer whilst it is being used by the unmanaged code.

It sounds like you wish to do 1. Have you benchmarked the delay? If it is the copying (how big is the buffer out of interest) then can you change your c# code to interact with the buffer as a byte pointer (and length)?
If you have access to C++/CLI you can often work around this more cleanly since it is easier to have parts of the code unmanaged (working with the raw buffer) and the parts that are managed just work with sub sections of it (copying only what is absolutely required)

长安忆 2024-07-30 12:20:02

有关 PInvoke 速度慢的主要原因以及如何解决此问题,请参阅 抑制UnmanagedCodeSecurityAttribute

For the main reason PInvoke is slow and what to use to solve this see SuppressUnmanagedCodeSecurityAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文