如何从 C# 获取非托管全局变量?
我正在为非托管 DLL 编写 .NET 包装器。原始 DLL 是带有 C 垫片的 C++,基本上只是以 C 形式复制 API,因此语言绑定并不是那么痛苦。我已经为它编写了一个 Python 绑定,所以我知道我想要做的事情应该可行。
DLL 有许多作为全局成员导出的回调,即:
__declspec(dllexport) extern int (*some_callback)(int32_t*, uint32_t);
我需要将托管函数附加到此指针,但我不知道如何获取非托管库中的非函数资源。除非我是瞎子,否则 DllImport 只导入函数。
有没有一种 C# 方法可以做到这一点,或者我需要用 C 编写一个提供注册函数的小垫片 DLL?我讨厌这种方法,因为它感觉不优雅,但如果我必须这样做,我就必须这样做。
I'm writing a .NET wrapper for an unmanaged DLL. The original DLL is C++ with a C shim that basically just replicates the API in C form, so that language bindings aren't such a pain. I've already written a Python binding for it, so I know what I'm trying to do should work.
The DLL has a number of callbacks exported as global members, viz.:
__declspec(dllexport) extern int (*some_callback)(int32_t*, uint32_t);
I need to attach a managed function to this pointer, but I can't figure out how to get at non-function resources in an unmanaged library. Unless I'm just blind, DllImport only imports functions.
Is there a C# way to do this, or do I need to write a little shim DLL in C that provides registration functions? I hate that approach, because it just feels inelegant, but if I have to, I have to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,P/Invoke 无法处理从 DLL 导出的数据。然而,通过直接获得出口在技术上是可能的。这非常丑陋,有一天您可能会后悔,但这有效:
示例 C/C++ DLL:
测试 C# 代码:
You're right, P/Invoke cannot handle data exports from a DLL. It is however technically possible by obtaining the export directly. This is very ugly and you'll probably regret it some day, but this worked:
Sample C/C++ DLL:
Test C# code: