[英]Marshal global variable in C#
我在C代码中有3个全局变量:一个int和两个指向结构的指针。 我必须在C#代码中将它们编组为公共静态成员,但我找不到任何解决方案。 我必须提到修改C代码是绝对的最后选择。
我必须整理全球:
extern const msdk_FileSystemInterface* m_fileInterface;
这是我到目前为止所尝试的,但不编译:
[DllImport("foo.so", EntryPoint = "m_fileInterface")]
private static extern IntPtr _m_fileInterface { get; set; }
public static msdk_FileSystemInterface m_fileInterface
{
get
{
return (msdk_FileSystemInterface)Marshal.PtrToStructure(_m_fileInterface, typeof(msdk_FileSystemInterface));
}
}
。
[DllImport("foo.so", EntryPoint = "m_fileInterface")]
private static extern IntPtr _m_fileInterface;
public static msdk_FileSystemInterface m_fileInterface
{
get
{
return (msdk_FileSystemInterface)Marshal.PtrToStructure(_m_fileInterface, typeof(msdk_FileSystemInterface));
}
}
msdk_FileSystemInterface结构在C#代码中可用(已经封送)。
有没有人有一个marsahalling全局变量的解决方案或是不可能的,我真的必须将它们包装在一个结构中或添加setter和getters到C代码?
您可以使用GetProcAddress获取导出的全局变量的地址。 你不能让编组人员用DllImport来做这件事。
所以你的两个选择是:
我个人会选择选项2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.