如何从 C# 访问非托管数据

发布于 2024-09-13 09:05:12 字数 301 浏览 2 评论 0原文

我有一个非托管 (C/C++) DLL,需要从 C# 应用程序调用。

DLL 调用需要将数据(从 DLL 创建的 C++ 类)返回到 C# 应用程序。

我可以控制 DLL 的导出函数,因此如果需要,我可以围绕 C++ 类创建一个包装器。我知道如何使用 p/Invoke 访问 DLL,因此我只是在寻找有关如何返回数据的一些指导。

从 DLL 接收到的数据是一系列键值对,因此我正在考虑返回 JSON 字符串并使用 Json.NET 将其反序列化为对象。

从 C#/.NET 应用程序的非托管源访问此类数据的首选方法是什么?

I have an unmanaged (C/C++) DLL which I need to call from a C# application.

The DLL call needs to return data (from a C++ class that is created by the DLL) to the C# application.

I have control over the DLL's exported function, so I can create a wrapper around the C++ class if necessary. I know how to access the DLL with p/Invoke, so I'm just looking for some guidance on how to return the data.

The data that is received from the DLL is a series of key-value pairs, so I was thinking about returning a JSON string and deserializing it to an object with Json.NET.

What is the preferred way to access this type of data from an unmanaged source from a C#/.NET application?

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

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

发布评论

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

评论(2

扛刀软妹 2024-09-20 09:05:12

如果您能够修改 C++ DLL,那么 C++/CLI 可能是最干净的。您将能够直接用 C++ 编写一组 .NET 包装器类,从而无需编写单独的 P/Invoke 声明。

If you're able to modify the C++ DLL then C++/CLI could be cleanest. You'll be able to write a set of .NET wrapper classes directly in C++, avoiding the need to write separate P/Invoke declarations.

吾性傲以野 2024-09-20 09:05:12

没有真正的理由添加额外的序列化层只是为了在托管和非托管应用程序之间传输代码。但是,如果它对您的代码来说更简单并且您不担心性能,那么将数据从一层移动到另一层的任何方式都是完全可以的。

如果您想尝试 PInvoke 方式,这里有来自 MSDN 的一些资源...

There is no real reason to add extra layers of serialization just to transfer code between managed and unmanaged applications. But if it would be simpler for your code and you aren't worried about performance than any way you can move data from one layer to another is perfectly fine.

If you want to try the PInvoke way here are a few resources from MSDN...

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