C#调用C++动态库接口函数和回调函数方法 后续

声明回调委托,C#的委托可以实现C#调用C++的回调,操作函数以后的回调

//定义委托,CallingConvention.StdCall可以,CallingConvention.Cdecl不行,参考https://www.it1352.com/1792610.html
 //[UnmanagedFunctionPointer(CallingConvention.Cdecl)] //不需要要添加该句话,具体参考 //https://blog.csdn.net/weixin_30786657/article/details/98678227
 public delegate int CallBackGWQStartSWithRec(int ErrorCode, string SignPdfBase64, string SignNameBase64, string FingerPrintBase64, string XML, string endTime);
注意:
其中WINAPI也称为StdCall不像大多数C / C ++库通常使用的Cdecl。
CallingConvention默认是CallingConvention.StdCall

动态库声明

public class GWQDllHidDevice
    {
        [DllImport("GWQDll.dll", EntryPoint = "GWQ_StartSWithRec")]
        public extern static int GWQ_StartSWithRec(string PDFPath, int SignType, string Location, string VideoPath, int Timeout, int FPWidth, int SignWidth, CallBackGWQStartSWithRec Q_StartSignWithRec, byte[] VoiceText, int VoiceTextLen);
    }

使用

public class  GWQDevice {
 public static int CallBackSWithRec(int ErrorCode, string SPdfBase64, string SNameBase64, string FPBase64, string XML, string endTime)
        {
//处理回调内容
            try
            {
                if (ErrorCode == 0)
                {
                    if (!string.IsNullOrEmpty(SPdfBase64))
                    {
                        var bytes = Convert.FromBase64String(SPdfBase64);
                        using (FileStream fs = new FileStream("signPDFmerge.pdf", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            fs.Write(bytes, 0, bytes.Length);
                            Console.WriteLine("sPDFmerge.pdf 写入当前目录");
                        }
                        var spdfBase64 = SPdfBase64;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"操作以后回调失败,原因:{ex}");
            }return ErrorCode;
        }


 public int GWQStartSignWithRecAsync(string PDFPath, string Location, string VideoPath, int Timeout, int FPWidth, int SignWidth, string VoiceText, int SignType = 1)
        {
            int ret = 0;
            try
            {
                byte[] VoiceTextbyte = null;
                if (string.IsNullOrWhiteSpace(VoiceText))
                {
                    VoiceTextbyte = default;// new byte[];                    
                }
                else
                {
                    VoiceTextbyte = Encoding.GetEncoding("GB18030").GetBytes(VoiceText);
                }
                CallBackGWQStartSWithRec Q_StartSWithRec = new CallBackGWQStartSWithRec(CallBackSWithRec);
                ret = GWQDllHidDevice.GWQ_StartSWithRec(PDFPath, SignType, Location, VideoPath, Timeout, FPWidth, SignWidth, Q_StartSWithRec, VoiceTextbyte, VoiceTextbyte.Length);
            }
            catch
            {
            }
            finally
            {
                //Finished();
            }
            return ret;
        }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值