我正在为非托管 C dll 编写 C# 绑定(bind)。
dll 提供了 5 个钩子(Hook)来返回几个数据:
typedef void (*t_libpd_printhook)(const char *recv);
并导出一个字段,如:
EXTERN t_libpd_printhook libpd_printhook;
现在我正在使用 Interop Assistant生成绑定(bind)代码,它只给了我一个委托(delegate)定义:
public delegate void t_libpd_printhook([In] [MarshalAs(UnmanagedType.LPStr)] string recv);
那么我可以使用一些神奇的 Interop 函数调用来设置 DLL 中的 t_libpd_printhook 字段吗?
最佳答案
您可以使用 LoadLibrary
和 GetProcAddress
获取指向导出的 libpd_printhook
变量的指针。然后,您可以使用 Marshal.WriteIntPtr
和 Marshal.GetFunctionPointerForDelegate
分配给委托(delegate)。
[DllImport("kernel32", SetLastError=true, CharSet=CharSet.Unicode)]
static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true,
SetLastError=true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool FreeLibrary(IntPtr hModule);
.....
IntPtr lib = LoadLibrary(@"mydll.dll");
IntPtr plibpd_printhook = GetProcAddress(lib, "libpd_printhook");
Marshal.WriteIntPtr(plibpd_printhook,
Marshal.GetFunctionPointerForDelegate(mydelegate));
FreeLibrary(lib);
您将想要添加我为了简化示例而删除的错误检查。
现在,如果您可以控制非托管库,我仍然建议添加一个函数来封装对此函数指针的写入。这对我来说是一个更好的界面。
关于c# - 从 C# 设置非托管 C dll 的公共(public)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10066550/