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. redis之五大数据类型介绍

    目录 redis五大数据类型 1. string(字符串) 特点: 格式: 基本操作: 2. hash(哈希) 特点: 格式: 基本操作 3. list(列表) 特点 格式 基本操作 4. set(集 ...

  2. 初识IntPtr------转载

    初识IntPtr 一:什么是IntPtr 先来看看MSDN上说的:用于表示指针或句柄的平台特定类型.这个其实说出了这样两个事实,IntPtr 可以用来表示指针或句柄.它是一个平台特定类型.对于它的解释 ...

  3. C 随机数产生

    // ConsoleApplication5.cpp : Defines the entry point for the console application. // #include " ...

  4. P1250 种树 题解

    题目描述 一条街道的一边有几座房子,因为环保原因居民想要在路边种些树,路边的居民被分割成 n 块,并被编号为 1…n.每块大小为一个单位尺寸并最多可种一棵树.每个居民想在门前种些树并指定了三个数b,e ...

  5. 解题报告:luogu P5536 【XR-3】核心城市

    题目链接:P5536 [XR-3]核心城市 这题是某次月赛题. 这题我完全是看标签猜的. 优先选择直径中点即可,这里重要的是互通,很容易想到用堆维护可选的,预处理直径和距叶节点距离即可(最近),实质上 ...

  6. 《ES6标准入门》(阮一峰)--12.Symbol

    1.概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种 ...

  7. Net Core3.1 添加 Swagger

    一.为什么使用Swagger 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远. 前端和后端的唯一联系,变成了 ...

  8. APM概述

    当前 APM(Application Performance Management (Monitoring)/ 应用性能管理)或者叫 Distributed Tracing System 工具有: G ...

  9. SQLlite的olestr

    关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...

  10. Netty 模型

    Demo代码 使用Maven的话请在pom.xml中注入netty依赖 <!-- https://mvnrepository.com/artifact/io.netty/netty-all -- ...