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);
- }
- }
- }
Delphi写的DLL回调C#的更多相关文章
- Delphi写的DLL,OCX中多线程一个同步问题
Delphi写的DLL,OCX中如果使用了TThread.Synchronze(Proc),可能导致线程死锁,原因是无法唤醒EXE中主线程, Synchronze并不会进入EXE主线程消息队列. 下面 ...
- 发现个delphi调用vc写的Dll中包括pchar參数报错奇怪现象
发现个delphi调用vc写的Dll中包括pchar參数奇怪现象 procedure中的第一行语句不能直接调用DLL的函数,否则会执行报错,在之前随意加上条语句就不报错了奇怪! vc的DLL源代码地址 ...
- Delphi调用c++写的dll (me)
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- abap调vb写的dll实现电子天平的读数(带控件版)
废话不多说,直接上. 鉴于abap调研的dll文件需要在wins注册,自己尝试过delphi和C#感觉不是很好,最后毅然选择了VB来写 因为需要用到MScomm控件,所以对于将要写的dll需要带for ...
- Delphi 使用之dll文件生成与调用
DLL是Dynamic-Link Libraries(动态链接库)的缩写,库里面是一些可执行的模块以及资源(如位图.图标等).可以认为DLL和EXE基本上是一回事,只是DLL不能直接执行,而必须由应用 ...
- 深入Delphi下的DLL编程
深入Delphi下的DLL编程 作者:岑心 引 言 相信有些计算机知识的朋友都应该听说过“DLL”.尤其是那些使用过windows操作系统的人,都应该有过多次重装系统的“悲惨”经历——无论再怎样小心, ...
- Delphi7程序调用C#写的DLL解决办法(转)
近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行: 编写C#dll的方法都一样,首先在vs2005中创建一个 ...
- 【转载】java调用C++写的DLL
用java调用C++写的DLL一直以来都是一个比较麻烦但又很常见的问题. 我们知道,使用 JNI 调用 .dll/.so 共享类库是非常非常麻烦和痛苦的. 如果有一个现有的 .dll/.so 文件,如 ...
- Delphi7程序调用C#写的DLL解决办法
近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行: 编写C#dll的方法都一样,首先在vs2005中创建一 ...
随机推荐
- 模板volist自增变量
- BZOJ 1046 最长不降子序列(nlogn)
nlogn的做法就是记录了在这之前每个长度的序列的最后一项的位置,这个位置是该长度下最后一个数最小的位置.显然能够达到最优. BZOJ 1046中里要按照字典序输出序列,按照坐标的字典序,那么我萌可以 ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
- mac平台多个php版本快速切换
mac平台多个php版本快速切换 要求所有php版本都是由brew安装 使用brew安装php多版本方法 # brew install php56 # brew install php70 安装切换工 ...
- UILabel加载HTML
NSString *string1 = @"<font color = \"red\">什么情况</font><br/>"; ...
- Android 学习第16课,java 包、类等相关的一些基础知识
1.建议将类放在包中,不要使用无名包 2.建议包名都用小写单词组成,不要用大写 3.建议包名用“域名的倒写.项目名.模块名”的形式,以确保包名的唯一性 注意:类变量与实例变量.类方法与实例方法的区别 ...
- 转载:ORA-01438: 值大于为此列指定的允许精度
Number的数据声明如下: 表示 作用 说明Number(p, s) 声明一个定点数 p(precision)为精度,s(scale)表示小数 ...
- 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验
转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...
- 二叉树[C实现]
#include<stdio.h> #include<malloc.h> #include<iostream> //定义节点 typedef struct BiNo ...
- 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 ...