Delphi 调用C# DLL(包含委托)
例子
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(包含委托)的更多相关文章
- delphi 调用c#dll
public interface iProduct { string Buy(); } [ClassInterface(ClassInterfaceType.None)] public class P ...
- Delphi 使用之dll文件生成与调用
DLL是Dynamic-Link Libraries(动态链接库)的缩写,库里面是一些可执行的模块以及资源(如位图.图标等).可以认为DLL和EXE基本上是一回事,只是DLL不能直接执行,而必须由应用 ...
- Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)
Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...
- .net DLL 注册 regasm delphi调用
.net DLL 注册 regasm regasm regasm myTest.dll regasm.exe 打开vs2005自带的工具“Visual Studio 2005命令提示”,输入上述命令 ...
- Delphi XE3写DLL,用Delphi7调用,报错!
http://bbs.csdn.net/topics/390870532 用delphi xe3写的DLL,delphi7调用,参数都是PAnsiChar,DLL里的函数接收delphi7传的入参,没 ...
- Delphi调用C# 编写dll动态库
Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...
- 发现个delphi调用vc写的Dll中包括pchar參数报错奇怪现象
发现个delphi调用vc写的Dll中包括pchar參数奇怪现象 procedure中的第一行语句不能直接调用DLL的函数,否则会执行报错,在之前随意加上条语句就不报错了奇怪! vc的DLL源代码地址 ...
- Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8)
delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__std ...
- Delphi - Windows系统下,Delphi调用API函数和7z.dll动态库,自动把文件压缩成.tar.gz格式的文件
项目背景 应欧美客户需求,需要将文件压缩成.tar.gz格式的文件,并上传给客户端SFTP服务器. 你懂的,7-Zip软件的显著特点是文件越大压缩比越高,在Linux系统上相当于我们Windows系统 ...
随机推荐
- About_PHP_数据类型&常用数组函数
PHP数据类型总结: 1:Boolean 布尔类型 返回值就是true和false 特殊情况:(1)false (2)整型0的时候 (3)空字符串/字符串“0” (4)null 以上都会被返回fals ...
- ajax pagination 布局刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- log4j详解(一)
>>>>1. 概述<<<< 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的:监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统 ...
- UUID
首先我们要知道UUID是什么?有什么用?为什么要使用它? UUID是什么? UUID 的目的,是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定.如此一来,每个 ...
- cxf webservice简单应用
Server端 server部署到一个端口号为80的tomcat中 CXFController.java package com.lwj.controller; import java.io.IOEx ...
- 关于linux开机进入grub问题的解决方法
用还是用ls (hd0,X)/grub命令查看每个盘里面的内容, 情况一 :如果你是在/boot/grub这个目录下找到的 grub rescue>root=(hd0,9) gr ...
- C# 控制datagridview的combox属性的列绑定数据
//datagridvie列绑定list的数据 List<User> listChange = GetChange();//查询数据库内容,保存到list this.datagridvie ...
- visual studio code 里调试运行 Python代码
最近对微软的visual studio code 挺感兴趣的,微软的跨平台开发工具.轻量简洁. 版本迭代的也挺快的,截止16年8月2日已经1.3.1版本了,功能也愈加完善.(16年12月18日 已经, ...
- Android Handler机制
http://blog.csdn.net/erliangcode/article/details/52117831
- hive --service metastore 出现的问题
Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083 执行命令jps root@hadoopm:/usr# jps1763 Res ...