C#调用C++ dll的回调函数

1 C++ Dll动态库中接口及回调函数定义

/****************************************************************************
    Function  :  Callback to get priview data
    Parameters:  pPreviewData - [out] preview data pointer        [allocated in the DLL]
                 nWidth       - [out] preview data width pointer  [allocated in the DLL]
                 nHeight      - [out] preview data height pointer [allocated in the DLL]
*****************************************************************************/
typedef void(__stdcall* CallbackFinger)(unsigned char *pPreviewData, 
                                        int *nWidth, int *nHeight);                                      
/****************************************************************************
    Function  :  Get fingerprint image
    Parameters:  nPrintType      - [in]  Capture Type.
                 nMissNum        - [in]  Number of missing fingers.
                 nTimeOutMs      - [in]  Timeout time, unit: Ms.
                 pFingerData     - [out] Image data pointer.
                 nFingerWidth    - [out] Image Width
                 nFingerHeight   - [out] Image Height
                 pCallback       - [out] Callback data. (if null, no preview)                     
    Return    :  0     - success
             Other - failed
*****************************************************************************/
int __stdcall captureFinger(int nPrintType,int nMissNum,int nTimeOutMs,
        unsigned char *pFingerData, int *nFingerWidth, int *nFingerHeight,
        CallbackFinger pData);
        
/************************************************************************
    Function  : Open the device
    Parameters: szModelPath - [in] Model file path(without include Chinese),
                                   if null, model file is in current directory
    Return    : 0     - success
                Other - failed
************************************************************************/
int __stdcall openDevice(char* szModelPath=NULL);
 
/************************************************************************
    Function  : Close the device
    Parameters:
    Return    : 0     - success
                Other - failed
************************************************************************/
int __stdcall closeDevice();

2 C#实现

2.1 调用dll的接口类

using System;
using System.Runtime.InteropServices;

namespace FAP60Demo
{
    //定义回调函数的委托 
    public delegate void CallbackDelegate(IntPtr pPreviewData, int[] w, int[] h);

    class mxDevFunc
    {
        [DllImport("mxFAP60.dll", EntryPoint = "openDevice")]
        public static extern int openDevice(string szModelPath);

        [DllImport("mxFAP60.dll", EntryPoint = "closeDevice")]
        public static extern int closeDevice();

        [DllImport("mxFAP60.dll", EntryPoint = "captureFinger")]
        public static extern int captureFinger(int printType, int nMissNum, int unTimeOutMs,
            byte[] pFingerData, int[] w,int[] h, CallbackDelegate call);
    }
}

2.2 回调函数的实现

        public void CallbackFunc(IntPtr pPreviewData, int[] w, int[] h)
        {
            int len = w[0] * h[0];
            byte[] fingerData = new byte[len];
            Marshal.Copy(pPreviewData, fingerData, 0, len);
            // 处理 fingerData 里的内容
            Bitmap bmap = AssistantFunc.CreateBitmap(fingerData, w[0], h[0]);
            Image img = Image.FromHbitmap(bmap.GetHbitmap());
            pictureBox_preview.Image = img;
            pictureBox_preview.Refresh();
        }

2.3 测试代码

        public int testCallback()
        {
            int captureType = 0;
            int nMissNum    = 0;
            int unTimeout = 10 * 1000;
            byte[] pFingerData = new byte[1600 * 1500];
            int[] w = new int[1];
            int[] h = new int[1];
            CallbackDelegate myDelegate = new CallbackDelegate(CallbackFunc);
            int nRet = mxDevFunc.captureFinger(captureType, nMissNum, unTimeout, 
                                               pFingerData, w, h, myDelegate);
            if (nRet == 0)
            {
                // 处理 fingerData 里的内容
                Bitmap bmap = AssistantFunc.CreateBitmap(pFingerData, w[0], h[0]);
                Image img = Image.FromHbitmap(bmap.GetHbitmap());
                pictureBox_preview.Image = img;
                pictureBox_preview.Refresh();
            }
            return nRet;
        }

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值