声明回调委托,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;
- }
- }
-
-