8-29 c#调用含回调和句柄的c++程序

1.内容简述

 1  [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
 2         public delegate void detectCallBack(byte[] pchType, byte[] pchEventName, byte[] pchEventTime, byte[] pchData);
 3 
 4         //[DllImport(@"C:\人脸检测\csxtym_veryhuo.com32 -V2 - 副本\csxtym\bin\Debug\dev_camera.dll", EntryPoint = "DVR_StartRealPlay", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
 5         //public static extern int StartRealPlay(int hUserHandle, Delegate callback);
 6 
 7          [DllImport(@"C:\人脸检测\csxtym_veryhuo.com32 -V2 - 副本\csxtym\bin\Debug\dev_camera.dll", EntryPoint = "OpenCamera", CallingConvention = CallingConvention.Cdecl)]
 8         public static extern void OpenCamera (
 9         int nID_Color, /* 彩色摄像头ID,暂保留*/
10         int nID_BW, /* 黑白摄像头ID,暂保留*/
11         System.IntPtr wndHandle, /* 父窗口句柄*/
12         int left, /* 子窗口在父窗口中的坐标及大小*/
13         int top,
14         int height,
15         int width,
16         Delegate callback, /* 回调函数*/
17         byte[] pchErrCode, /* 返回码,成功,非失败*/
18         byte[] pchError /* 返回描述*/
19         ) ;
20 
21          [DllImport(@"C:\人脸检测\csxtym_veryhuo.com32 -V2 - 副本\csxtym\bin\Debug\dev_camera.dll", EntryPoint = "StartDetect", CallingConvention = CallingConvention.Cdecl)]
22          public static extern int StartDetect
23          (
24           int detectMode, /* 检测难度,
25            threshold=10,20,30 */
26           int photoHeight, /* 图像高,暂保留*/
27           int photoWidth, /* 图像宽,暂保留*/
28           System.IntPtr ptrIn, /* 保存照片全路径*/
29           int expressRate, /* 照片压缩率*/
30           int timeout, /* 检测超时时间*/
31            byte[] pchErrCode, /* 返回码,成功,非失败*/
32            byte[] pchError
33          );

 

     1.传句柄参数

          1.获得控件句柄

            比如获取textbox 的句柄,只需textbox1.Handle

          2.c++ dll 函数声明

          

[DllImport(@"C:\人脸检测\csxtym_veryhuo.com32 -V2 - 副本\csxtym\bin\Debug\dev_camera.dll", EntryPoint = "StartDetect", CallingConvention = CallingConvention.Cdecl)]
22          public static extern int StartDetect
23          (
24           int detectMode, /* 检测难度,
25            threshold=10,20,30 */
26           int photoHeight, /* 图像高,暂保留*/
27           int photoWidth, /* 图像宽,暂保留*/
28           System.IntPtr ptrIn, /* 保存照片全路径*/
29           int expressRate, /* 照片压缩率*/
30           int timeout, /* 检测超时时间*/
31            byte[] pchErrCode, /* 返回码,成功,非失败*/
32            byte[] pchError
33          );

 

          3.调用函数

 

     2.传回调函数

          1.回调函数声明

           2.调用回调函数的函数声明

           3.与回调函数结构相同的委托声明

           4.委托的实例化

           5.委托的具体实现函数

           6.将5中的函数赋值给4

出现问题

       1.如果参数是数组如下面代码中的31,32,不用在前面加ref。如果写成如下格式就错了

ref byte[] pchErrCode


DllImport(@"C:\人脸检测\csxtym_veryhuo.com32 -V2 - 副本\csxtym\bin\Debug\dev_camera.dll", EntryPoint = "StartDetect", CallingConvention = CallingConvention.Cdecl)]
22          public static extern int StartDetect
23          (
24           int detectMode, /* 检测难度,
25            threshold=10,20,30 */
26           int photoHeight, /* 图像高,暂保留*/
27           int photoWidth, /* 图像宽,暂保留*/
28           System.IntPtr ptrIn, /* 保存照片全路径*/
29           int expressRate, /* 照片压缩率*/
30           int timeout, /* 检测超时时间*/
31            byte[] pchErrCode, /* 返回码,成功,非失败*/
32            byte[] pchError
33          );

 2.如果回调函数返回的是字符串的话,那么回调函数对应的变量就写成string即可,不用写成char[],或者byte[]。‘

如下面

 

byte[] pchType, byte[] pchEventName, byte[] pchEventTime, byte[] pchData

如上写法就是错的,应该把byte[]改成string

UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
 2         public delegate void detectCallBack(byte[] pchType, byte[] pchEventName, byte[] pchEventTime, byte[] pchData);

 

posted @ 2017-08-29 21:27  飞扬吧青春  阅读(1253)  评论(0编辑  收藏  举报