C#如何调用C++DLL中参数有结构体数组指针的函数?

C++DLL对应.h头文件结构体部分:
#define IN [ in ]
#define OUT [ out ]
#define OPTIONAL [ defaultvalue(0) ]
//导出函数声明中会用到

typedef struct tag_CXC_ACTION_FORMAT
{
long maskValid;
long maskChanged;
float nScaleMin;
float nScaleMax;
float nThreshold1;
float nThreshold2;
}
CXC_ACTION_FORMAT;

C++DLL对应.h头文件声明,导出函数声明部分:
extern "C" declspec(dllimport) BOOL stdcall GetFormats(
OUT CXC_ACTION_FORMAT * pFormats,
IN OUT int * pcFormats ,
IN OPTIONAL int iStart );

说明:GetFormats这个函数用于返回对应数据的所有格式(最大值、最小值、阈值等等)。pFormats是一个包含最新数据的(结构体)数组;pcFormats表示要更新的数组的最后一个元素;iStarts是pFormat中第一个更新的元素。

大致的情况如上,在调用的时候我试过以下几种方法调用,但是要么在使用的时候出错,要么在得到的时候错误的数据,请问大神们该如何声明和调用?

//DLL函数调用声明
[DllImport("ConnectionClient", EntryPoint = "cxcGetFormats", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
//下为结构体声明,数据类型转化过
public struct CXC_ACTION_FORMAT
{
public Int32 maskValid;
public Int32 maskChanged;
public Single nScaleMin;
public Single nScaleMax;
public Single nThreshold1;
public Single nThreshold2;
}
//下为函数调用声明
//public extern unsafe static bool GetFormats(out CXC_ACTION_FORMAT* pFormats, out IntPtr pcFormats,int iStart);//方式一
//public extern unsafe static Byte GetFormats(out IntPtr pFormats, out IntPtr pcFormats, Int32 iStart);//方式二
//public extern unsafe static Byte GetFormats(out CXC_ACTION_FORMAT pFormats, ref IntPtr pcFormats, Int32 iStart);//方式四
//public extern unsafe static Byte GetFormats(out CXC_ACTION_FORMAT pFormats, out IntPtr pcFormats, Int32 iStart);//方式五

里面涉及到函数指针,在C#里面用委托替代,总的代码如下: delegate int pfunc(void* dst,void* src,int nSize); unsafe public struct MyStruct { public Byte* pMemory;//也可以用unsinged int替代(uint*) public pfunc myfunc;//这里用委托替代函数指针 public char[] rd;//声明的时候不能指定大小,可以在new的时候指定大小 }
温馨提示:答案为网友推荐,仅供参考
相似回答