【发布时间】:2011-12-04 11:42:30
【问题描述】:
我有一个使用 C++ 编写的非托管 dll。我可以从我的 C# 应用程序中轻松调用一些函数。但是一个功能让我受苦:)
C++
问题出在日志参数中。它应该反映为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)
C#(我的转换)
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!
有什么想法吗?
【问题讨论】:
-
调用它会发生什么?堆栈不平衡?
AccessViolationException?静默失败? -
静默失败..没有帮助