例子

C# Dll:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics; using System.Runtime.InteropServices; public delegate void ProcessDelegate(long ptr, long len); namespace TestDll
{ public interface ITestClass2
{ void YourProcedure();
void SetDelegate(long ptr);
} [ClassInterface(ClassInterfaceType.None)]
public class Class2 : ITestClass2
{
private ProcessDelegate delProcess = null; public void YourProcedure()
{
Debug.Print("enter");
if (this.delProcess != null)
{
byte[] test = System.Text.Encoding.Default.GetBytes("hello");
GCHandle hObject = GCHandle.Alloc(test, GCHandleType.Pinned);
IntPtr pObject = hObject.AddrOfPinnedObject();
this.delProcess(pObject.ToInt64(), test.Length);
if (hObject.IsAllocated)
hObject.Free();
}
Debug.Print("end");
} public void SetDelegate(long ptr)
{
IntPtr intPrt = new IntPtr(ptr);
delProcess = (ProcessDelegate)Marshal.GetDelegateForFunctionPointer(intPrt, typeof(ProcessDelegate));
} } }

注意的一点 一定要如下设置:

另外, 如果需要导入其他库的话, 项目-->添加引用;

Delphi调用:

首先要注册dll,  命令行至C:\Windows\Microsoft.NET\Framework\v4.0.30319

运行 regasm "你的dll路径\名称.dll"

提示注册成功就可以使用这个dll了

uses ComObj;

procedure callBack(swide: Int64; size: Int64); stdcall;
begin
OutputDebugString(PChar(IntToStr(swide)+':'+ IntToStr(size)));
end; procedure TForm1.btn1Click(Sender: TObject);
var aClass: Variant;
begin
aClass:= CreateOleObject('TestDll.Class2');
aClass.SetDelegate(Dword(@call));
aClass.YourProcedure('test');
end;

示例: 下载地址

Delphi 调用C# DLL(包含委托)的更多相关文章

  1. delphi 调用c#dll

    public interface iProduct { string Buy(); } [ClassInterface(ClassInterfaceType.None)] public class P ...

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

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

  3. Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)

    Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...

  4. .net DLL 注册 regasm delphi调用

    .net DLL 注册 regasm regasm regasm myTest.dll regasm.exe 打开vs2005自带的工具“Visual Studio 2005命令提示”,输入上述命令 ...

  5. Delphi XE3写DLL,用Delphi7调用,报错!

    http://bbs.csdn.net/topics/390870532 用delphi xe3写的DLL,delphi7调用,参数都是PAnsiChar,DLL里的函数接收delphi7传的入参,没 ...

  6. Delphi调用C# 编写dll动态库

    Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...

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

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

  8. Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8)

    delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__std ...

  9. Delphi - Windows系统下,Delphi调用API函数和7z.dll动态库,自动把文件压缩成.tar.gz格式的文件

    项目背景 应欧美客户需求,需要将文件压缩成.tar.gz格式的文件,并上传给客户端SFTP服务器. 你懂的,7-Zip软件的显著特点是文件越大压缩比越高,在Linux系统上相当于我们Windows系统 ...

随机推荐

  1. shared jedis 在spring中的配置

    redis 属性文件配置: redis.host=xx.xx.xx.xx redis.port=6379 #redis.pass=xxxxx redis.maxIdle=10000 redis.max ...

  2. bzoj4511:[Usaco2016 Jan]Subsequences Summing to Sevens

    题目大意:给个序列,求最长的连续子序列使其为7的倍数 又是一道令人欢喜的不用怎么用脑的水题.. 边读入,边计算前缀和 分别保存前缀和%7结果为1,2,3,4,5,6的第一次的位置 然后减一减就知道长度 ...

  3. Java 中多条件排序

    Collections.sort(ghEntityList, new Comparator<GongHuiEntity>() { @Override public int compare( ...

  4. centos6.5新增加硬盘挂载并实现开机时自动挂载

    在内网主机新增一个2T硬盘,先关机断电再连接硬盘数据线和电源线! 查看当前磁盘设备信息: [root@tb ~]# fdisk -lWARNING: GPT (GUID Partition Table ...

  5. Rails--抛出异常

    begin ... rescue Exception => e ... end

  6. git SourceTree常用操作

    1.git 修改用户名以及提交邮箱 修改用户名和邮箱有两种方式,第一种 git 修改当前的project的用户名的命令为:git config user.name 你的目标用户名;  git 修改当前 ...

  7. 一起来做chrome扩展《AJAX请求》

    chrome在一次更新之后,出于安全考虑,完全的禁止了content_script从https向http发起ajax请求,即使正常情况下也会在console里给出提示.这对于WEB来讲是好事,但对于扩 ...

  8. URL转Drawable之 Android中获取网络图片的三种方法

    转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...

  9. Java-马士兵动态代理模式

    Java-马士兵动态代理模式 模拟jdk的动态代理的实现原理, 这些东西没有必要写出来,写项目的时候一般用不上,主要是为了面试和理解原理: java动态代理有什么作用 作用非常大,在很多底层框架中都会 ...

  10. git review出现的问题

    在提交代码review的时候可能会出现 Could not connect to gerrit.Enter your gerrit username: xxxxTrying again with ss ...