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编写与调用的更多相关文章

  1. DLL编写与调用全解

    DLL编写与调用全解 DELPHI学习   2008-12-23 22:52   阅读8   评论0   字号: 大  中  小 第一章 为什么要使用动态链接库(DLL) top 提起DLL您一定不会 ...

  2. VB.NET中的DLL编写和调用的最简单示例

    DLL(动态链接库)是一个很有用的东西,在开发大项目的时候显得非常重要,因为多人合作开发时,可以给每个人分配一个任务,用DLL完成,最后组合起来,就不会出现互相冲突的问题.这里给出最简单的DLL编写与 ...

  3. delphi dll创建及调用

    第一章 DLL简单介绍由于在目前的学习工作中,需要用到DLL文件,就学习了下,在这里作个总结.首先装简单介绍下DLL:1,减小可执行文件的大小DLL技术的产生有很大一部分原因是为了减小可执行文件的大小 ...

  4. Delphi编写DLL供C#调用的实例

    Delphi中编写的Dll: library TestDLL; { Important note about DLL memory management: ShareMem must be the f ...

  5. delphi 基础之三 编写和调用dll文件

    delphi 编写和调用dll文件   Windows 的执行文件可以划分为两种形式程序和动态连接库 (DLLs).一般程序运行是用.EXE文件,但应用程序有时也可以调用存储在DLL的函数. 在如下几 ...

  6. delphi编写与调用DLL(delphi7下测试通过)

    http://blog.sina.com.cn/s/blog_4dbbf76f01000anz.html delphi编写DLL 下面在delphi中编写一个简单的dll,在该dll中只有一个max函 ...

  7. Delphi DLL的创建、静态及动态调用

    转载:http://blog.csdn.net/welcome000yy/article/details/7905463 结合这篇博客:http://www.cnblogs.com/xumenger/ ...

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

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

  9. Delphi 动态与静态调用DLL(最好的资料)

    摘要:本文阐述了 Windows 环境下动态链接库的概念和特点,对静态调用和动态调用两种调用方式作出了比较,并给出了 Delphi 中应用动态链接库的实例. 一.动态链接库的概念    动态链接库(  ...

随机推荐

  1. 分析配置DispatcherServlet类时load-on-startup标签作用

    <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org. ...

  2. 使 Firefox 和 Vivaldi 只在新标签页显示书签栏

    Firefox 新建 ~/.mozilla/firefox/rre9emvh.default/chrome/userChrome.css (大概不同人的 rre9emvh.default 目录会有不同 ...

  3. VMware安装CentOS操作系统详细步骤

    目录 创建虚拟机(买电脑) 1. 创建新虚拟机 2. 自定义配置虚拟机 3. 选择虚拟机硬件兼容性 4. 安装虚拟机创建系统 5. 选择虚拟机操作系统 6. 设置虚拟机名字和存放位置 7. 设置虚拟机 ...

  4. 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值

    「BZOJ3676」[Apio2014] 回文串   Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...

  5. C#学习之time控件和timer_tick事件 -----转载

    Timer控件:Timer控件只有绑定了Tick事件,和设置Enabled=True后才会自动计时,停止计时可以用Stop()控制,通过Stop()停止之后,如果想重新计时,可以用Start()方法来 ...

  6. js 实现循环遍历数组

    for in循环遍历 let arr = [1, 2, 3, 4, 4, 3], str = '' for (const val in arr) { str += val + ' ' } consol ...

  7. Linux用户和用户组管理命令

    一.用户管理命令 1.useradd   创建用户或更新默认新用户的信息 使用方法  useradd [options] 用户名 选项: useradd -u           指定UID具体数值, ...

  8. Docker 安装(centos7下)

    下面链接为官方的安装方法(官方的是最好的): https://docs.docker.com/install/linux/docker-ce/centos/#upgrade-docker-after- ...

  9. 使用 MYSQLBINLOG 来恢复数据

    使用 MYSQLBINLOG 来恢复数据 2009-04-05 12:47:05 标签:mysql mysqlbinlog 恢复 数据库 数据 原创作品,允许转载,转载时请务必以超链接形式标明文章 原 ...

  10. Windows Mysql Server重启, log-bin路径配置

    Windows Mysql Server重启, log-bin路径配置 分类: mysql数据库2014-03-16 14:49 1313人阅读 评论(0) 收藏 举报 Mysqlmysql serv ...