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 中应用动态链接库的实例. 一.动态链接库的概念 动态链接库( ...
随机推荐
- Django(二十)分页:
一.知识点 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/pagination/ 查询出所有省级地区的信息,显示在页面上. AeroInfo ...
- Python 数组
使用之前要先导入函数库 import numpy as np 数组名=np.zeros(数组大小,数据类型) 初始化为0值,这里的数据类型只能是数值类型,字符类型不能用 一.一维数组 impo ...
- No qualifying bean of type 'org.springframework.ui.Model' available
原因:@Autowired 下面没有注入类
- nginx 安装部署前篇
官网:https://nginx.org/ 特性:既可以作为HTTP服务器,也可以作为反向代理服务器或者邮件服务器或者邮件服务器:能够快递响应静态页面的请求:支持 Fast CGI.SSL.Virtu ...
- DeepLearning算法文章
算法源码: learn_dl : https://github.com/hanbt/learn_dl rnn-from-scratch : https://github.com/pangolulu/r ...
- 怎样实现android 返回到上一个Activity并重新执行一次onCreate方法
1.onCreate 方法只在activity一开始创建的时候执行.2.也就是在该activity销毁后才能再次执行,假如当前activity上再打开一个activity,并且原来的activity已 ...
- Spring事务原理分析-部分一
Spring事务原理分析-部分一 什么事务 事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败. 事务基本特性 ⑴ 原子性(Atomicity) 原子性是指事务包含的所有操作要 ...
- JDBC--DBUtils的使用
1.commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性 ...
- Maven项目- get请求的乱码问题,使用tamcat7出现乱码的解决方法
get请求的乱码问题: 解决方法: 手动处理编码
- 深度解析标点符号在Report写作中的应用
准确的标点符号和大写字母可以帮助Tutor准确理解report的意思.标点符号的某些方面,例如使用逗号,可以是一种个人风格,在引号中正确的标点符号是至关重要的.在前面的一些文章当中我们也给大家说了re ...