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; } }
龙腾一族至尊龙骑
合集:
C#调用C++动态库接口函数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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通讯成功案例