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

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

  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#部分

  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. }

Delphi写的DLL回调C#的更多相关文章

  1. Delphi写的DLL,OCX中多线程一个同步问题

    Delphi写的DLL,OCX中如果使用了TThread.Synchronze(Proc),可能导致线程死锁,原因是无法唤醒EXE中主线程, Synchronze并不会进入EXE主线程消息队列. 下面 ...

  2. 发现个delphi调用vc写的Dll中包括pchar參数报错奇怪现象

    发现个delphi调用vc写的Dll中包括pchar參数奇怪现象 procedure中的第一行语句不能直接调用DLL的函数,否则会执行报错,在之前随意加上条语句就不报错了奇怪! vc的DLL源代码地址 ...

  3. Delphi调用c++写的dll (me)

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  4. abap调vb写的dll实现电子天平的读数(带控件版)

    废话不多说,直接上. 鉴于abap调研的dll文件需要在wins注册,自己尝试过delphi和C#感觉不是很好,最后毅然选择了VB来写 因为需要用到MScomm控件,所以对于将要写的dll需要带for ...

  5. Delphi 使用之dll文件生成与调用

    DLL是Dynamic-Link Libraries(动态链接库)的缩写,库里面是一些可执行的模块以及资源(如位图.图标等).可以认为DLL和EXE基本上是一回事,只是DLL不能直接执行,而必须由应用 ...

  6. 深入Delphi下的DLL编程

    深入Delphi下的DLL编程 作者:岑心 引 言 相信有些计算机知识的朋友都应该听说过“DLL”.尤其是那些使用过windows操作系统的人,都应该有过多次重装系统的“悲惨”经历——无论再怎样小心, ...

  7. Delphi7程序调用C#写的DLL解决办法(转)

    近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:    编写C#dll的方法都一样,首先在vs2005中创建一个 ...

  8. 【转载】java调用C++写的DLL

    用java调用C++写的DLL一直以来都是一个比较麻烦但又很常见的问题. 我们知道,使用 JNI 调用 .dll/.so 共享类库是非常非常麻烦和痛苦的. 如果有一个现有的 .dll/.so 文件,如 ...

  9. Delphi7程序调用C#写的DLL解决办法

     近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:    编写C#dll的方法都一样,首先在vs2005中创建一 ...

随机推荐

  1. 模板volist自增变量

  2. BZOJ 1046 最长不降子序列(nlogn)

    nlogn的做法就是记录了在这之前每个长度的序列的最后一项的位置,这个位置是该长度下最后一个数最小的位置.显然能够达到最优. BZOJ 1046中里要按照字典序输出序列,按照坐标的字典序,那么我萌可以 ...

  3. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

  4. mac平台多个php版本快速切换

    mac平台多个php版本快速切换 要求所有php版本都是由brew安装 使用brew安装php多版本方法 # brew install php56 # brew install php70 安装切换工 ...

  5. UILabel加载HTML

    NSString *string1 = @"<font color = \"red\">什么情况</font><br/>"; ...

  6. Android 学习第16课,java 包、类等相关的一些基础知识

    1.建议将类放在包中,不要使用无名包 2.建议包名都用小写单词组成,不要用大写 3.建议包名用“域名的倒写.项目名.模块名”的形式,以确保包名的唯一性 注意:类变量与实例变量.类方法与实例方法的区别 ...

  7. 转载:ORA-01438: 值大于为此列指定的允许精度

    Number的数据声明如下: 表示        作用        说明Number(p, s)        声明一个定点数        p(precision)为精度,s(scale)表示小数 ...

  8. 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验

    转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...

  9. 二叉树[C实现]

    #include<stdio.h> #include<malloc.h> #include<iostream> //定义节点 typedef struct BiNo ...

  10. 3.Complementing a Strand of DNA

    Problem In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'. The r ...