delphi dll编写与调用
dll代码:
mydll.dpr
library mydll; uses
System.SysUtils,
System.Classes,
uFunction in 'uFunction.pas'; {$R *.res} exports
AddInt,
AddStr,
AddDouble,
Add; end.
uFunction.pas
unit uFunction; interface uses
System.SysUtils, System.Classes, Winapi.Messages; function AddInt(a1: Integer; a2: Integer): Integer; stdcall;
function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall;
function AddDouble(a1: Double; a2: Double): Double; stdcall;
function Add(a1: Integer; a2: Integer): PWideChar; stdcall; implementation function AddInt(a1: Integer; a2: Integer): Integer; stdcall;
begin
Result := a1 + a2;
end; function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall;
begin
Result := PWideChar(string(a1) + string(a2));
end; function AddDouble(a1: Double; a2: Double): Double; stdcall;
begin
result:= a1 + a2;
end; function Add(a1: Integer; a2: Integer): PWideChar; stdcall;
begin
Result := PWideChar((a1 + a2).ToString);
end; end.
delphi调用:
unit uMain; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; function AddInt(a1: Integer; a2: Integer): Integer; stdcall; external 'mydll.dll';
function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall; external 'mydll.dll';
function AddDouble(a1: Double; a2: Double): Double; stdcall; external 'mydll.dll';
function Add(a1: Integer; a2: Integer): PWideChar; stdcall; external 'mydll.dll'; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(AddInt(, ).ToString);
end; procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(string(AddStr('hello ', 'world')));
end; procedure TForm1.Button3Click(Sender: TObject);
begin
ShowMessage(AddDouble(5.1, 6.1).ToString);
end; procedure TForm1.Button4Click(Sender: TObject);
begin
ShowMessage(string(Add(, )));
end; procedure TForm1.FormCreate(Sender: TObject);
begin
Position := poScreenCenter;
end; end.
c#调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace hello_dll
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int AddInt(int a1, int a2); [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr AddStr(string a1, string a2); [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern double AddDouble(double a1, double a2); [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr Add(int a1, int a2); private void Form1_Load(object sender, EventArgs e)
{
StartPosition = FormStartPosition.CenterScreen;
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(AddInt(, ).ToString());
} private void button2_Click(object sender, EventArgs e)
{
IntPtr ptr = AddStr("hello ", "world");
string str = Marshal.PtrToStringUni(ptr);
MessageBox.Show(str);
} private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(AddDouble(5.1, 6.1).ToString());
} private void button4_Click(object sender, EventArgs e)
{
IntPtr ptr = Add(, );
string str = Marshal.PtrToStringUni(ptr);
MessageBox.Show(str);
}
}
}
delphi dll编写与调用的更多相关文章
- DLL编写与调用全解
DLL编写与调用全解 DELPHI学习 2008-12-23 22:52 阅读8 评论0 字号: 大 中 小 第一章 为什么要使用动态链接库(DLL) top 提起DLL您一定不会 ...
- VB.NET中的DLL编写和调用的最简单示例
DLL(动态链接库)是一个很有用的东西,在开发大项目的时候显得非常重要,因为多人合作开发时,可以给每个人分配一个任务,用DLL完成,最后组合起来,就不会出现互相冲突的问题.这里给出最简单的DLL编写与 ...
- delphi dll创建及调用
第一章 DLL简单介绍由于在目前的学习工作中,需要用到DLL文件,就学习了下,在这里作个总结.首先装简单介绍下DLL:1,减小可执行文件的大小DLL技术的产生有很大一部分原因是为了减小可执行文件的大小 ...
- Delphi编写DLL供C#调用的实例
Delphi中编写的Dll: library TestDLL; { Important note about DLL memory management: ShareMem must be the f ...
- delphi 基础之三 编写和调用dll文件
delphi 编写和调用dll文件 Windows 的执行文件可以划分为两种形式程序和动态连接库 (DLLs).一般程序运行是用.EXE文件,但应用程序有时也可以调用存储在DLL的函数. 在如下几 ...
- delphi编写与调用DLL(delphi7下测试通过)
http://blog.sina.com.cn/s/blog_4dbbf76f01000anz.html delphi编写DLL 下面在delphi中编写一个简单的dll,在该dll中只有一个max函 ...
- Delphi DLL的创建、静态及动态调用
转载:http://blog.csdn.net/welcome000yy/article/details/7905463 结合这篇博客:http://www.cnblogs.com/xumenger/ ...
- Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)
Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...
- Delphi 动态与静态调用DLL(最好的资料)
摘要:本文阐述了 Windows 环境下动态链接库的概念和特点,对静态调用和动态调用两种调用方式作出了比较,并给出了 Delphi 中应用动态链接库的实例. 一.动态链接库的概念 动态链接库( ...
随机推荐
- 使用Thymeleaf时,ajax的url如何设置?
使用Thymeleaf时,ajax的url如何设置? 最近在做一个论坛项目使用到了Thymeleaf,在使用ajax请求的时候发现无法获取BasePath.在经过一番查阅资料后终于得知如下俩种方法,在 ...
- python的init函数里参数的作用
问题发现 一直有一个疑问,有时我们在继承时,在__init__函数会出现可变参数*arg或关键字参数**kw这样的参数,这些参数有什么用?如果有用,这些参数时如何传递?传递些什么? 注:如果你不知道什 ...
- Python自动化运维的职业发展道路(暂定)
Python职业发展之路 Python自动化运维工程 Python基础 Linux Shell Fabric Ansible Playbook Zabbix Saltstack Puppet Dock ...
- vagrant 添加带版本号的 box
众所周知,vagrant添加box的时候要从外网下载,那速度...(说多了都是泪),所以只好用下载工具下载到本地之后再添加. 如何搭建 homestead:https://laravelacademy ...
- 5.4 Linux 安装2个tomcat
Linux系统下怎样配置多个Tomcat同时运行呢,首先第一个tomcat配置不变,然后修改第二个tomcat启动的脚本 拷贝第一个tomcat的目录到第二个tomcat目录 [root@eshop- ...
- java对象转换String类型的三种方法
在很多情况下我们都需要将一个对象转换为String类型.一般来说有三种方法可以实现:Object.toString().(String)Object.String.valueOf(Object).下面 ...
- 2.Jsoup
public static void main(String[] args) { //爬取最大资源网上的数据 //用CSS选择器 try { Document doc = Jsoup.parse(ne ...
- MQTT 协议学习: 总结 与 各种定义的速查表
背景 经过几天的学习与实操,对于MQTT(主要针对 v3.1.1版本)的学习告一段落,为了方便日后的查阅 本文链接:<MQTT 协议学习: 总结 与 各种定义的速查表> 章节整理 MQTT ...
- windows制作动态链接库和使用二
动态库的另一种制作方法: 不使用_declspec(dllexport)关键字,使用.def文件 //exportFun.def 文件名随意 EXPORT add @ //格式 函数名 @编号 < ...
- PAN3501与AS3933完美兼容替代
现在不少校园门禁卡都是采用奥地利的AS3933,市场需求是供不应求,当然价格上还是不断上升趋势.成本上压力也是越来越大,不少厂家在寻找能替代软硬件兼容AS3933的芯片方案.今天我就为大家介绍一款能否 ...