[英]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.