简单叙述一下我的问题:我用C#调用C++编写的动态库,有一个函数的参数是结构体指针,而那个结构体中又嵌套其他的结构体。调用之后发现传递过去的数值不正确,废话不多说,直接上图!希望牛人帮忙解答!
这是C++SDK接口函数
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
P_CLIENT_API int CALL_METHOD P_Client_QueryRecord(P_Client_QueryRecInfo* pInfo);
这是提供的C++ Demo结构体定义
struct P_Client_TIME
{
DWORD dwYear; //年
DWORD dwMonth; //月
DWORD dwDay; //日
DWORD dwHour; //时
DWORD dwMinute; //分
DWORD dwSecond; //秒
};
// 录像查询条件
struct P_Client_QueryRecInfo
{
char szChannelID[P_CLIENT_MAX_ID_LEN]; //通道ID
int nSource; //来源 enum_P_Record_Source
int nRecordType; //类型 enum_P_Record_Type
char szCardInfo[P_CLIENT_MAX_ID_LEN]; //卡号(类型为卡号录像时有效)
P_Client_TIME sStartTime; //起始时间
P_Client_TIME sEndTime; //结束时间
};
这是提供的C++Demo调用SDK
P_Client_QueryRecInfo queryInfo= {0};
queryInfo.nRecordType = m_cmbType.GetCurSel();
queryInfo.nSource = m_cmbSource.GetCurSel();
strcpy(queryInfo.szChannelID, m_szCameraID );
queryInfo.sStartTime.dwYear = m_beginDate.GetYear();
queryInfo.sStartTime.dwMonth = m_beginDate.GetMonth();
queryInfo.sStartTime.dwDay = m_beginDate.GetDay();
queryInfo.sStartTime.dwHour = m_beginTime.GetHour();
queryInfo.sStartTime.dwMinute = m_beginTime.GetMinute();
queryInfo.sStartTime.dwSecond = m_beginTime.GetSecond();
queryInfo.sEndTime.dwYear = m_endDate.GetYear();
queryInfo.sEndTime.dwMonth = m_endDate.GetMonth();
queryInfo.sEndTime.dwDay = m_endDate.GetDay();
queryInfo.sEndTime.dwHour = m_endTime.GetHour();
queryInfo.sEndTime.dwMinute = m_endTime.GetMinute();
queryInfo.sEndTime.dwSecond = m_endTime.GetSecond();
int nHandle = P_Client_QueryRecord( &queryInfo );
if ( nHandle > 0 )
{
MessageBox("query successful");
}
下面是我用C#写的调用接口
//定义接口
[DllImport("ClientSDK.dll", EntryPoint = "P_Client_QueryRecord", CallingConvention = CallingConvention.StdCall)]
public static extern int P_Client_QueryRecord(ref P_Client_QueryRecInfo pInfo);
//定义结构体
[StructLayout(LayoutKind.Sequential)]
public struct P_Client_TIME
{
public int dwYear; //年
public int dwMonth; //月
public int dwDay; //日
public int dwHour; //时
public int dwMinute; //分
public int dwSecond; //秒
};
// 录像查询条件
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct P_Client_QueryRecInfo
{
public string szChannelID; //通道ID
public int nSource; //来源 enum_P_Record_Source
public int nRecordType; //类型 enum_P_Record_Type
public string szCardInfo; //卡号(类型为卡号录像时有效)
public P_Client_TIME sStartTime; //起始时间
public P_Client_TIME sEndTime; //结束时间
};
//赋值并调用接口
myClientHelper.P_Client_QueryRecInfo queryInfo = new myClientHelper.P_Client_QueryRecInfo();
queryInfo.nSource = 1;//0代表平台,1代表设备
queryInfo.nRecordType = cbType.SelectedIndex;
queryInfo.szChannelID = szChannelID;
queryInfo.szCardInfo = szChannelID;
queryInfo.sStartTime.dwYear = date1.Value.Year;
queryInfo.sStartTime.dwMonth = date1.Value.Month;
queryInfo.sStartTime.dwDay = date1.Value.Day;
queryInfo.sStartTime.dwHour = time1.Value.Hour;
queryInfo.sStartTime.dwMinute = time1.Value.Minute;
queryInfo.sStartTime.dwSecond = time1.Value.Second;
queryInfo.sEndTime.dwYear = date2.Value.Year;
queryInfo.sEndTime.dwMonth = date2.Value.Month;
queryInfo.sEndTime.dwDay = date2.Value.Day;
queryInfo.sEndTime.dwHour = time2.Value.Hour;
queryInfo.sEndTime.dwMinute = time2.Value.Minute;
queryInfo.sEndTime.dwSecond = time2.Value.Second;
int nHandle = myClientHelper.P_Client_QueryRecord(ref queryInfo);
if (nHandle > 0)
{
AddLog("查询录像成功");
}
现在没有报错信息,只是传过去的数据不对,无法正常调去接口里面的函数。
char szChannelID[P_CLIENT_MAX_ID_LEN]; //通道ID
这个应该 指定其长度
例如:
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string szChannelID; //通道ID
char szCardInfo[P_CLIENT_MAX_ID_LEN];
这个也是同样的
其他具体的 仔细比较一下 。
可以用sizeof(检查一下 C#结构体的大小是否和C++的一致)