【发布时间】:2014-06-10 15:19:52
【问题描述】:
我正在尝试将来自 c dll 的指针转换为其等效的 C# struct 数组。
C 代码
RECORD locked[MAX+1]; //MAX is a constant
typedef struct
{
State state; //enum
unsigned long allocated;
unsigned long lastUsed;
unsigned int useCount;
} RECORD;
API RECORD* __stdcall GetLocks( char* password )
{
if(strcmp(password, secret) == 0)
return locked;
else
return 0;
}
C#代码
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] // Pretty sure CharSet isnt actually needed here
public struct RECORD
{
public State state;
public UInt32 allocated;
public UInt32 lastUsed;
public UInt16 useCount;
}
[DllImport("gatewayapi.dll", CharSet = CharSet.Ansi)] // or here
static extern IntPtr GetLocks(string password);
public RECORD[] GetLocks(string password)
{
RECORD[] recs = new RECORD[MAX+1];
recs =(RECORD[])Marshal.PtrToStructure( GetLocks(password), typeof(RECORD[]));
if (recs.Length == 0)
{
throw new Exception();
}
return recs;
}
不幸的是,上面返回了一个 MissingMethodException -> 没有为此对象定义无参数构造函数。
所以我对 Marshalling 来说是 100% 的新手,并且希望得到一些关于如何将我从 C 接收到的指针转换为它所代表的实际 C# 结构数组的建议。
谢谢
【问题讨论】:
-
你不能在数组类型上使用 Marshal.PtrToStructure(),你必须一次做一个。 useCount 字段的类型错误,它是 int。
标签: c# c++ c marshalling