C# 委托,带有对 c++ 的字符串引用打回来

发布于 2024-08-11 19:43:08 字数 288 浏览 3 评论 0原文

我编写了一个 C# 应用程序,它通过托管 c++ dll 使用非托管 c++ dll。 在非托管 dll 中,有一个回调,其参数之一是 std::string&

我似乎找不到用托管 dll 包装它的正确方法。 当我使用 String^ 时,回调有效,但 C# 应用程序没有获取字符串中的任何内容。 当我使用 String^% 时,其他似乎不相关的地方开始崩溃(可能是内存损坏)。

所以我的问题是,这样做的正确方法是什么?

谢谢

I wrote a C# application that uses an unmanaged c++ dll via managed c++ dll.
In the unmanaged dll, there's a callback that one of its params is std::string&.

I can't seem to find the right way to wrap this with the managed dll.
When I use String^, the callback works, but the C# application does not get anything in the string.
When I used String^%, things started to crash in other places that does not seem to be related (maybe memory corruption).

So my question is, what's the right way to do this?

Thanks

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

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

发布评论

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

评论(3

隔纱相望 2024-08-18 19:43:08

我无法在这里复制粘贴代码,但我会尝试再次解释。
我无法在托管 c++ 部分中使用封送处理,因为我没有调用函数,而是传递 ac# 委托以进行回调。

在非托管 dll 中,我有一个回调,需要这样的函数:void Func(unsigned int, int, std:string &)

我的目标是将 ac# 委托从我的程序传递给该回调,因此在非托管代码中,
我做了一个像这样的委托:delegate void DEL(unsigned int a, int b, String ^ c)和一个像这样的函数:void mFunc(DEL ^ del),和该函数将委托编组到非托管回调订阅函数接受的 cb 中。 unsigned int 和 int 工作正常,但触发 C# 函数时字符串始终为 ""

I can't copy-paste code here, but I'll try to explain again.
I can't use marshaling in the managed c++ section because I'm not calling a function, but passing a c# delegate for a callback.

In the unmanaged dll, I have a callback that requires a function like this: void Func(unsigned int, int, std:string &).

My goal is to pass a c# delegate from my program to that callback, so in the unmanaged code,
I made a delegate like this: delegate void DEL(unsigned int a, int b, String ^ c) and a function like: void mFunc(DEL ^ del), and that function marshal's the delegate into a cb that the unmanaged callback subscribe function accepts. The unsigned int and int works fine, but the string is always "" when the C# function is triggered.

莫多说 2024-08-18 19:43:08

发布一些代码将帮助我更好地理解并给出更好的答案;但 String^ 不会自动转换或编组到 std::string。您需要自己进行编组才能将字符串返回到 C# 代码。快速搜索可以提供有关如何执行此操作的详细信息。

http://msdn.microsoft.com/en-us/library/42zy2z41。 ASPX

Posting some code would help me understand better and give a better answer; but there is no automatic conversion or marshalling of String^ to std::string. You would need to do to marshalling yourself to get the string back to the C# code. A quick search can provide with details on how to do this.

http://msdn.microsoft.com/en-us/library/42zy2z41.aspx

烟柳画桥 2024-08-18 19:43:08

我不相信编组可以处理 std::string。我认为您需要创建自己的传递 char * 的回调,然后在两者之间编写粘合代码。

此外,一旦委托被编组到回调中,该回调就不会被视为对委托可能来自的对象的引用。因此,如果委托不是静态方法,则需要在非托管回调的生命周期内将其填充到某个地方。

I don't believe that the marshalling can deal with std::string. I think you need to make your own callback that passes char * and then write the glue code between the two.

Also, once the delegate is marshalled into a callback, that callback does not count as a reference to the object that the delegate might have been made from. So if the delegate is not a static method, you need to stuff it somewhere for the lifetime of the unmanaged callback.

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