Delphi写的DLL回调C#

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

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

[delphi]view plaincopy

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

C#部分

[csharp]view plaincopy

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