C# 结构体数组 C++ DLL

 大华人脸识别 C++文档

方法:当 _iCmdId =  6,_lpIn 为输入的结构体参数 FaceLibQuery ,_lpOut 为输出的结构体数组 FaceLibQueryResult

  _lpOut为FaceLibQueryResult数组,数组大小为每页个数,_iOutLen值为sizeof(FaceLibQueryResult),非数组大小。

结构体:

 

 C#调用

 [DllImport("NVSSDK.dll", SetLastError = true)]
public static extern Int32 NetClient_FaceConfig(Int32 _iLogonId, Int32 _iCmdId, Int32 _iChanNo, IntPtr _lpIn, Int32 _iInLen, IntPtr _lpOut, Int32 _iOutLen);
    [StructLayout(LayoutKind.Sequential)]
    struct FaceLibQuery
    {
        public int iSize;
        public int iChanNo;
        public int iPageNo;
        public int iPageCount;
    };
    [StructLayout(LayoutKind.Sequential)]
    struct FaceLibQueryResult
    {
        public int iSize;
        public int iChanNo;
        public int iTotal;
        public int iPageNo;
        public int iIndex;
        public int iPageCount;
        public FaceLibInfo tFaceLib;
    };
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    struct FaceLibInfo
    {
        public int iSize;
        public int iLibKey;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CommonLen.LEN_64)]
        public byte[] cName;
        public int iThreshold;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CommonLen.LEN_64)]
        public byte[] cExtrInfo;
        public int iAlarmType;
        public int iOptType;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CommonLen.LEN_UUID)]
        public byte[] cLibUUID;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CommonLen.LEN_64)]
        public byte[] cLibVersion;
    };

 

   FaceLibQuery tQuery = new FaceLibQuery(); 
   tQuery.iSize = Marshal.SizeOf(tQuery);
   tQuery.iChanNo = m_iChannelNo;
   tQuery.iPageNo = 0;     //要查询的页码,iPageNo >= 0。
   tQuery.iPageCount = 20;   //查询的每页个数,取值范围[1,20]。

   IntPtr ipQueryInfo = Marshal.AllocHGlobal(Marshal.SizeOf(tQuery));
   Marshal.StructureToPtr(tQuery, ipQueryInfo, true);//false容易造成内存泄漏

   //封送结构体数组,搭建输出的结构体数组
   FaceLibQueryResult[] tResult = new FaceLibQueryResult[20];
   for (int i = 0; i < tResult.Length; i++)
   {
       tResult[i] = new FaceLibQueryResult();
   }
   IntPtr ipResult = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FaceLibQueryResult)) * 20);

   int iRet = -1;
   iRet = NVSSDK.NetClient_FaceConfig(m_iLogonId, NVSSDK.FACE_CMD_LIB_QUERY, m_iChannelNo, ipQueryInfo, Marshal.SizeOf(tQuery),
           ipResult, Marshal.SizeOf(typeof(FaceLibQueryResult))); //还原搭建的输出的结构体数组 for (int i = 0; i < 20; i++) { IntPtr ptr = (IntPtr)((UInt32)ipResult +
              i * Marshal.SizeOf(typeof(FaceLibQueryResult))); tResult[i] = (FaceLibQueryResult)Marshal.PtrToStructure(ptr, typeof(FaceLibQueryResult)); Console.WriteLine("人脸库:" + Encoding.Default.GetString(tResult[i].tFaceLib.cName)); } Marshal.FreeHGlobal(ipQueryInfo); Marshal.FreeHGlobal(ipResult);

 

posted @ 2020-03-13 11:21  超级驼鹿  阅读(684)  评论(0编辑  收藏  举报
/*