Delphi写的DLL回调C#

C#的调用Delphi的DLL没有问题,DLL回调时遇到了麻烦,网上找了个方法,解决了这个问题

Delphi部分,列举了三种回调函数定义

[delphi] view plain copy
  1. library test;  
  2.   
  3. uses  
  4.   SysUtils;  
  5.   
  6. {$R *.res}  
  7.   
  8. type  
  9.   TCallback = procedure (P: PChar); stdcall;  
  10.   TMethodCallback = procedure (P: PChar) of object; stdcall;  
  11.   
  12. procedure DllTest1(Callback: TCallback; P: PChar; I: Integer); stdcall;  
  13. var  
  14.   S: string;  
  15. begin  
  16.   S := Format('DllTest1 ''%s'' %d', [P, I]);  
  17.   if Assigned(Callback) then  
  18.     Callback(PChar(S));  
  19. end;  
  20.   
  21. procedure DllTest2(_Callback: Pointer; P: PChar; I: Integer); stdcall;  
  22. var  
  23.   Callback: TMethodCallback absolute _Callback;  
  24.   S: string;  
  25. begin  
  26.   S := Format('DllTest2 ''%s'' %d', [P, I]);  
  27.   if Assigned(Callback) then  
  28.     Callback(PChar(S));  
  29. end;  
  30.   
  31. procedure DllTest3(Callback: TMethodCallback; P: PChar; I: Integer); stdcall;  
  32. var  
  33.   S: string;  
  34. begin  
  35.   S := Format('DllTest3 ''%s'' %d', [P, I]);  
  36.   if Assigned(Callback) then  
  37.     Callback(PChar(S));  
  38. end;  
  39.   
  40. exports  
  41.   DllTest1,  
  42.   DllTest2,  
  43.   DllTest3;  
  44.   
  45. begin  
  46. end.  


C#部分

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5.   
  6. namespace DllTest  
  7. {  
  8.     class Program  
  9.     {  
  10.         public struct Method  
  11.         {  
  12.             public IntPtr code;  
  13.             public IntPtr data;  
  14.         }  
  15.         [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest1")]  
  16.         public static extern void DllTest1(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
  17.         [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest2")]  
  18.         public static extern void DllTest2(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
  19.         [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest3")]  
  20.         public static extern void DllTest3(Method m, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
  21.   
  22.         public delegate void Callback([MarshalAs(UnmanagedType.LPStr)] string s);  
  23.         public delegate void MethodCallback(IntPtr self, [MarshalAs(UnmanagedType.LPStr)] string s);  
  24.         public static void ShowInfo(string s)  
  25.         {  
  26.             Console.WriteLine("Info: " + s);  
  27.         }  
  28.         public static void ShowMethodInfo(IntPtr self, string s)  
  29.         {  
  30.             Console.WriteLine("Info: " + s);  
  31.         }  
  32.   
  33.   
  34.         static void Main(string[] args)  
  35.         {  
  36.             Method m;  
  37.             Callback info = ShowInfo;  
  38.             MethodCallback methodInfo = ShowMethodInfo;  
  39.             IntPtr p = Marshal.GetFunctionPointerForDelegate(info);  
  40.             IntPtr pm = Marshal.GetFunctionPointerForDelegate(methodInfo);  
  41.   
  42.             // function callback example  
  43.             DllTest1(p, "test", 42);  
  44.             // method callback example 1  
  45.             DllTest2(pm, "test", 42);  
  46.             // method callback example 2  
  47.             m.code = pm;  
  48.             m.data = IntPtr.Zero;  
  49.             DllTest3(m, "test", 42);  
  50.         }  
  51.     }  
  52. }  
原文地址:https://www.cnblogs.com/ljl_falcon/p/5915063.html