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


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

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

    动态库声明

    1. public class GWQDllHidDevice
    2. {
    3. [DllImport("GWQDll.dll", EntryPoint = "GWQ_StartSWithRec")]
    4. 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);
    5. }

    使用

    1. public class GWQDevice {
    2. public static int CallBackSWithRec(int ErrorCode, string SPdfBase64, string SNameBase64, string FPBase64, string XML, string endTime)
    3. {
    4. //处理回调内容
    5. try
    6. {
    7. if (ErrorCode == 0)
    8. {
    9. if (!string.IsNullOrEmpty(SPdfBase64))
    10. {
    11. var bytes = Convert.FromBase64String(SPdfBase64);
    12. using (FileStream fs = new FileStream("signPDFmerge.pdf", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
    13. {
    14. fs.Write(bytes, 0, bytes.Length);
    15. Console.WriteLine("sPDFmerge.pdf 写入当前目录");
    16. }
    17. var spdfBase64 = SPdfBase64;
    18. }
    19. }
    20. }
    21. catch (Exception ex)
    22. {
    23. logger.Error($"操作以后回调失败,原因:{ex}");
    24. }return ErrorCode;
    25. }
    26. public int GWQStartSignWithRecAsync(string PDFPath, string Location, string VideoPath, int Timeout, int FPWidth, int SignWidth, string VoiceText, int SignType = 1)
    27. {
    28. int ret = 0;
    29. try
    30. {
    31. byte[] VoiceTextbyte = null;
    32. if (string.IsNullOrWhiteSpace(VoiceText))
    33. {
    34. VoiceTextbyte = default;// new byte[];
    35. }
    36. else
    37. {
    38. VoiceTextbyte = Encoding.GetEncoding("GB18030").GetBytes(VoiceText);
    39. }
    40. CallBackGWQStartSWithRec Q_StartSWithRec = new CallBackGWQStartSWithRec(CallBackSWithRec);
    41. ret = GWQDllHidDevice.GWQ_StartSWithRec(PDFPath, SignType, Location, VideoPath, Timeout, FPWidth, SignWidth, Q_StartSWithRec, VoiceTextbyte, VoiceTextbyte.Length);
    42. }
    43. catch
    44. {
    45. }
    46. finally
    47. {
    48. //Finished();
    49. }
    50. return ret;
    51. }
    52. }

  • 相关阅读:
    MySQL系列索引专题
    Kolmogorov-Smirnov正态性检验
    【重识云原生】第六章容器基础6.4.9.6节——Service 与 Pod 的DNS
    单行函数,聚合函数课后练习
    右键菜单和弹出菜单的区别
    [附源码]计算机毕业设计springboot常见Web漏洞对应POC应用系统
    Python函数详解:参数、返回值和文档字符串
    Leetcode 284. Peeking Iterator (Iterator设计题)
    全国A级旅游景区清单数据(2023年更新)
    10 | apt 常用操作命令
  • 原文地址:https://blog.csdn.net/LongtengGensSupreme/article/details/134442811