C++/CLI 中的 void* 到 Object^

发布于 2024-07-29 01:24:55 字数 540 浏览 2 评论 0原文

我正在致力于将大量 .h 和 .lib 文件从本机 C++ 包装到托管 C++,以便最终用作 C# 中引用的 .dll。

一些本机 C++ 函数的返回类型为 void*。 当我将值传回调用代码时,我不确定如何处理这个问题。 例如:如果 C# 应用程序调用我的 dll 包装器,我会从本机调用返回什么:

void* start(ThreadFunc,void *, unsigned *);

我当前正在尝试将返回结果装箱到通用 System::Object^ 中,但没有成功。 这是包装器中的调用:

m_NativeThread->start(cb, 
        GCHandle::ToIntPtr(GCHandle::Alloc(o)).ToPointer(),
        static_cast<unsigned int*>(GCHandle::ToIntPtr(GCHandle::Alloc(u)).ToPointer())));

任何人都可以提供解决方案吗?

I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#.

Some of the native C++ functions have a return type of void*. I am not sure how to handle this when I pass back the value to my calling code. For instance: if a C# app calls my dll wrapper, what do I return from the native call:

void* start(ThreadFunc,void *, unsigned *);

I am currently attempting to box the return in a generic System::Object^ with no luck. This is the call in the wrapper:

m_NativeThread->start(cb, 
        GCHandle::ToIntPtr(GCHandle::Alloc(o)).ToPointer(),
        static_cast<unsigned int*>(GCHandle::ToIntPtr(GCHandle::Alloc(u)).ToPointer())));

Can anyone offer a solution?

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

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

发布评论

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

评论(2

懵少女 2024-08-05 01:24:55

如果您的托管代码需要查看 void* 中的数据:

您不能将 void* 转换为非托管类型
内存到托管对象的引用。
要将其转换为托管内存,
你必须使用 Marshal.Copy() 或
Marshal.PtrToStructure()。 那个意志
课程只有在您知道类型的情况下才有效
void* 指向的数据。
来源

如果您的托管代码不需要查看 void* 中的数据:

如果您将其存储在 IntPtr 中
托管代码不需要查看什么
它是,只是将其传递回
稍后非托管代码。
来源


If your managed code needs to see the data in the void*:

You can't cast a void* to unmanaged
memory to a managed object reference.
To turn this into managed memory,
you'd have to use Marshal.Copy() or
Marshal.PtrToStructure(). That will of
course only work if you know the type
of the data that the void* points to.
source

If your managed code doesn't need to see the data in the void*:

Store it in an IntPtr if your
managed code doesn't need to see what
it is and just passes it back into the
unmanaged code later on.
source

德意的啸 2024-08-05 01:24:55

你能把它变成一个 IntPtr 吗? 您希望客户如何处理 void*?

Can you make it an IntPtr? What do you expect the client to do with the void*?

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