Delphi编写DLL供C#调用的实例
Delphi中编写的Dll:
library TestDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes;
function ADD(X,Y:Integer):Integer;stdcall;
begin
Result :=X+Y;
end;
function Txx(x,y:Double):Double ;stdcall ;
begin
Result :=x+y+y+x;
end;
function EncryVncPassStrHex(Str: PChar): PChar;stdcall;
var
TempResult: String;
begin
TempResult := WideCharToString(str);
Result := PChar(TempResult);
end;
{$R *.res}
exports
ADD,
EncryVncPassStrHex,
Txx;
begin
end.
其中涉及到三个输出函数:一个输出的变量为整数,一个为浮点数,另一个为字符串。
C#中调用该Dll:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace UseDLL
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class UseDLL
{
[DllImport("TestDLL.DLL",EntryPoint="ADD",CharSet= CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int add(int x,int y);
[DllImport("TestDLL.DLL",EntryPoint="Txx",CharSet= CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern double PP(double x,double y);
[DllImport("TestDLL.DLL", EntryPoint="EncryVncPassStrHex",CharSet=CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern string EncryVncPassStrHex( string Str);
} public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent(); //
// TODO: Add constructor code after the InitializeComponent() call.
//
} void Button1Click(object sender, EventArgs e)
{
int x=;
int y=;
int z=UseDLL.add(x,y);
this.textBox1.Text=z.ToString();
double oo=0.123;
double pp=0.254;
this.button1.Text=UseDLL.PP(oo,pp).ToString();
this.Text =UseDLL.EncryVncPassStrHex("我是XXX");
}
}
}
注意:在C#的using部分必须添加“using System.Runtime.InteropServices”。
程序运行结果为:

本程序在delphi2010和SharpDevelop通过。
Delphi编写DLL供C#调用的实例的更多相关文章
- delphi编写dll心得, 谢谢原作者的分享。转
delphi编写dll心得 1.每个函数体(包括exports和非exports函数)后面加 'stdcall;', 以编写出通用的dll2.exports函数后面必须加'export;'(放在'st ...
- Go 程序编译成 DLL 供 C# 调用。
Go 程序编译成 DLL 供 C# 调用. C# 结合 Golang 开发 1. 实现方式与语法形式 基本方式:将 Go 程序编译成 DLL 供 C# 调用. 1.1 Go代码 注意:代码中 ex ...
- 用IKVMC将jar转成dll供c#调用
用IKVMC将jar转成dll供c#调用 ikvmc c# dll jar 用IKVMC将jar转成dll供c#调用 前言 ikvmc介绍 ikvmc下载安装 下载并解压 设置环境变量 jar-> ...
- 分享一次C#调用Delphi编写Dll程序
1.前言: 最近接手了一个项目需要和Delphi语言编写的一个系统进行一些接口的对接,数据在传输过程中采用Des加密方式,因为Delphi 平台的加密方式和C#平台的加密方式不互通,所以采用的方式是C ...
- Delphi 编写DLL动态链接库文件的知识
一.DLL动态链接库文件的知识简介: Windows的发展要求允许同时运行的几个程序共享一组函数的单一拷贝.动态链接库就是在这种情况下出现的.动态链接库不用重复编译或链接,一旦装入内存,Dlls函数可 ...
- Delphi 编写DLL动态链接库文件的知识和样例(有详细步骤,很清楚)
一.DLL动态链接库文件的知识简介: Windows的发展要求允许同时运行的几个程序共享一组函数的单一拷贝.动态链接库就是在这种情况下出现的.动态链接库不用重复编译或链接,一旦装入内存,Dlls函数可 ...
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)
最近这阵子,接了个私活,封装一个开发包俗称的SDK给客户调用,查阅了很多人家的SDK,绝大部分用VC编写,而且VC6.0居多,估计也是为了兼容大量的XP用户及IE浏览器,XP自带了VC6.0运行库,所 ...
- delphi 创建DLL文件 及其调用和注意事项
首先创建一个DLL文件,项目自带的代码为: library ProjectPnr; { Important note about DLL memory management: ShareMem mus ...
- [笔记]Delphi 2007写DLL供VC调用实例
考虑如下几种常用情况: - VC传入int,返回int- VC传入char *,返回int- VC传入char *,返回char *及int 为简化问题,传递的字符串参数只考虑ANSI格式,不考虑UN ...
随机推荐
- HelloWorld[Java]
public class HelloWorld{ public static void main(String args[]){ System.out.println("HelloWorld ...
- myeclipse激活时cracker2015.jar打不开
myeclipse激活时cracker2015.jar打不开 可能是jdk版本的问题 这是我用的myeclipse版本 myeclipse-2016-ci-7-offline-installer-wi ...
- MSSQL-实用小工具
1.创建查询辅助表 create table nums (n int not null) alter table nums add constraint PK_NUMS primary key clu ...
- C#调试心经(1)(转)
我们在做程序开发时,难免会遇到错误异常.如何快速地找到出错的地方.分析错误的原因以及找到解决问题的方案,是许多初级程序员困扰的问题,这也正是经验的宝贵之处.下面我将简单介绍在Visual Studio ...
- [Linux] - CentOS中文乱码解决办法
CentOS 7 终端中文乱码解决办法: 1.使用vim编辑locale.config文件: vim /etc/locale.conf 2.将LANG="en_US.UTF-8"修 ...
- windows下安装boost库
工作中现在会接触boost,所以我计划两个月之内努力熟悉一下boost.今天在自己win10系统上尝试安装了boost库,下面把遇到的问题总结一下: 1. 下好1.61版本库,在boost目录下运行b ...
- jquery 通知页面变化
var PageTitleNotification = { Vars: { OriginalTitle: document.title, Interval: null, IsNotificationE ...
- pc wap 判断浏览器ua属性
var ua = navigator.userAgent.toLowerCase(); var Android = String(ua.match(/android/i)) == "andr ...
- 短链(ShortURL)的Java实现
什么叫短链或短址? 就是把长的 URL 转成短的 URL, 现在提供这种服务的有很多公司,我们以google家的 URL shortener 服务: http://goo.gl/ 为例. 任何长网址都 ...
- 转 listener.log文件过大导致oracle数据库连接非常慢
数据库(31) 最近发现oracle数据库连接非常慢,sqlplus很快,用客户端就很慢,甚至会无响应. 然后服务器内存一下就飙升到了90%,不是表空间占满了,也不是数据库连接数占满了.重启还是一样 ...