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; } }
复制代码

 

posted @   龙骑科技  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2018-11-16 socket-WebSocket-HttpListener-TcpListener服务端客户端的具体使用案例
2018-11-16 HttpListener通讯成功案例
2018-11-16 Socket通讯成功案例
点击右上角即可分享
微信分享提示