![](/img/trans.png)
[英]Calling unmanaged C DLL function from C# (with pointer to struct and pointer to bool array)
[英]calling unmanaged dll function that returns an array of struct using C#
我正在使用C ++编写非托管dll。 我可以从C#应用程序轻松调用某些函数。 但是一个功能让我受苦:)
问题出在log参数中。 它应该反映为Data_Struct
类型的数组:
typedef struct
{
unsigned int id;
unsigned short year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char min;
unsigned char sec;
unsigned char status;
}Data_Struct;
int Read_Stored_Data(HUNIT pUnitHandle, int option, int updateFlag,
int maxEntries, unsigned char *log)
public struct Data_Struct
{
public uint id;
public ushort year;
public byte month;
public byte day;
public byte hour;
public byte min;
public byte sec;
public byte status;
}
[DllImport("SData.dll", EntryPoint = "Read_Stored_Data")]
public static extern int Read_Stored_Data(int pUnitHandle, int option,
int updateFlag, int maxEntries, ref Data_Struct[] log);
请假设我正在传递带有正确值的pUnitHandle
, option
, updateFlag
和maxEntries
。 问题是最后一个参数( log
):
Data_Struct[] logs = new Data_Struct[1000];
res = Read_Stored_Data(handle, 1, 0, 1000, ref logs); // This should work but it
// causes the application
// to terminate!
任何想法?
尝试使用PInvoke属性。
具体来说,将布局应用于结构:
[StructLayout(LayoutKind.Sequential)]
public struct Data_Struct
{
public uint id;
public ushort year;
public byte month;
public byte day;
public byte hour;
public byte min;
public byte sec;
public byte status;
}
并在删除ref
同时对参数应用编组属性:
[DllImport("SData.dll", EntryPoint = "Read_Stored_Data")]
public static extern int Read_Stored_Data(int pUnitHandle, int option,
int updateFlag, int maxEntries, [MarshalAs(UnmanagedType.LPArray), Out()] Data_Struct[] log);
看看是否有帮助,请进行相应调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.