Delphi写的DLL回调C#


本文整理自网络,侵删。

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

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

library test;  
  
uses  
  SysUtils;  
  
{$R *.res}  
  
type  
  TCallback = procedure (P: PChar); stdcall;  
  TMethodCallback = procedure (P: PChar) of object; stdcall;  
  
procedure DllTest1(Callback: TCallback; P: PChar; I: Integer); stdcall;  
var  
  S: string;  
begin  
  S := Format('DllTest1 ''%s'' %d', [P, I]);  
  if Assigned(Callback) then  
    Callback(PChar(S));  
end;  
  
procedure DllTest2(_Callback: Pointer; P: PChar; I: Integer); stdcall;  
var  
  Callback: TMethodCallback absolute _Callback;  
  S: string;  
begin  
  S := Format('DllTest2 ''%s'' %d', [P, I]);  
  if Assigned(Callback) then  
    Callback(PChar(S));  
end;  
  
procedure DllTest3(Callback: TMethodCallback; P: PChar; I: Integer); stdcall;  
var  
  S: string;  
begin  
  S := Format('DllTest3 ''%s'' %d', [P, I]);  
  if Assigned(Callback) then  
    Callback(PChar(S));  
end;  
  
exports  
  DllTest1,  
  DllTest2,  
  DllTest3;  
  
begin  
end.  

C#部分



using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Runtime.InteropServices;  
  
namespace DllTest  
{  
    class Program  
    {  
        public struct Method  
        {  
            public IntPtr code;  
            public IntPtr data;  
        }  
        [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest1")]  
        public static extern void DllTest1(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
        [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest2")]  
        public static extern void DllTest2(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
        [DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "DllTest3")]  
        public static extern void DllTest3(Method m, [MarshalAs(UnmanagedType.LPStr)] string s, int i);  
  
        public delegate void Callback([MarshalAs(UnmanagedType.LPStr)] string s);  
        public delegate void MethodCallback(IntPtr self, [MarshalAs(UnmanagedType.LPStr)] string s);  
        public static void ShowInfo(string s)  
        {  
            Console.WriteLine("Info: " + s);  
        }  
        public static void ShowMethodInfo(IntPtr self, string s)  
        {  
            Console.WriteLine("Info: " + s);  
        }  
  
  
        static void Main(string[] args)  
        {  
            Method m;  
            Callback info = ShowInfo;  
            MethodCallback methodInfo = ShowMethodInfo;  
            IntPtr p = Marshal.GetFunctionPointerForDelegate(info);  
            IntPtr pm = Marshal.GetFunctionPointerForDelegate(methodInfo);  
  
            // function callback example  
            DllTest1(p, "test", 42);  
            // method callback example 1  
            DllTest2(pm, "test", 42);  
            // method callback example 2  
            m.code = pm;  
            m.data = IntPtr.Zero;  
            DllTest3(m, "test", 42);  
        }  
    }  
}  

来源:https://www.cnblogs.com/ljl_falcon/

相关阅读 >>

Delphi中url的汉字编码

Delphi下的纯pascal的十六进制转十进制

Delphi从全路径中分离路径,有'\'

crc32.pas

Delphi 附加数据读取

Delphi 检测文件是否被占用

Delphi 记录类型- 结构指针

Delphi申请和释放内存

Delphi listview 设置固定列宽

Delphi 备份恢复剪切板

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...