C++ 接口:
BOOL CLIENT_QueryDevState(LLONG lLoginID, int nType, char *pBuf, int nBufLen, int *pRetLen, int waittime=1000);
C++ struct 1:
struct NET_PTZ_PRESET_LIST
{
DWORD dwSize;
DWORD dwMaxPresetNum;
DWORD dwRetPresetNum;
NET_PTZ_PRESET *pstuPtzPorsetList;
};
C++ struct 2:
struct NET_PTZ_PRESET
{
int nIndex;
char szName[PTZ_PRESET_NAME_LEN];
char szReserve[64];
};
我申明的接口:
[DllImport(GlobalConst.DHNETSDK_PATH, CallingConvention = CallingConvention.Cdecl)]
public static extern bool CLIENT_QueryDevState(long lLoginID, int nType, IntPtr pBuf, int nBufLen, ref int pRetLen, int waittime = 1000);
我申明的结构体 1:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct NET_PTZ_PRESET_LIST
{
public int dwSize;
public int dwMaxPresetNum;
public int dwRetPresetNum;
[MarshalAs(UnmanagedType.ByValArray)]
public NET_PTZ_PRESET[] pstuPtzPorsetList;
}
我申明的结构体 2:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct NET_PTZ_PRESET
{
public int nIndex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = GlobalConst.PTZ_PRESET_NAME_LEN)]
public string szName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szReserve;
}
我的调用:
int nType = GlobalConst.DH_DEVSTATE_PTZ_PRESET_LIST;
int pRetLen = 0;
int sizeList = Marshal.SizeOf(typeof(GlobalStruct.NET_PTZ_PRESET_LIST));
int bufSize = sizeList;
var pBuf = Marshal.AllocHGlobal(bufSize);
Marshal.StructureToPtr(nET_PTZ_PRESET_LIST, pBuf, true);
var result = DHSDK.CLIENT_QueryDevState(_loginID, nType, pBuf, bufSize, ref pRetLen, 3000);
if (!result)
{
Marshal.FreeHGlobal(pBuf);
var errorCode = GetLastError();
return;
}
var obj = (GlobalStruct.NET_PTZ_PRESET_LIST)Marshal.PtrToStructure(pBuf,
typeof(GlobalStruct.NET_PTZ_PRESET_LIST));
结果:要么接口直接报内存错误;要么接口调用失败,返回的error=7(用户参数不合法);要么回来的数据转成结构体后,里面的数据有问题。
请教下各位这个接口到底该怎么正确调用?
已解决,通过指针转结构体列表
我们没用过人家的预制点,只用了ptz
dh_sdk@dahuatech.com
发邮件问他,我开发的时候遇到什么问题就直接发邮件问他,不过邮件有些慢,但是都靠谱
@echo_lovely: 直接用私人邮箱发给他就可以吗?一般多久能回复呢?
@0ATH: 时间不定,而且人家有时候很晚都会回复你
@0ATH: 我就用的我的QQ邮箱
@echo_lovely: 好的,感谢!我这就试试。
貌似直接这样找并不行