Delphi编写DLL供C#调用的实例
2016-12-03 11:14
447 查看
Delphi中编写的Dll:
其中涉及到三个输出函数:一个输出的变量为整数,一个为浮点数,另一个为字符串。
C#中调用该Dll:
注意:在C#的using部分必须添加“using System.Runtime.InteropServices”。
程序运行结果为:
本程序在delphi2010和SharpDevelop通过。
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=10; int y=100; 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调用C# 编写dll动态库
- Delphi调用C# 编写dll动态库
- NN远程桌面SDK之Delphi开发源码
- (转载)Delphi TStringList的用法
- DELPHI XE6调用JAVA WEBSERVICE
- Delphi中怎么调用vb的DLL
- Delphi - 数组 详解
- 日志_测试代码_Delphi7
- delphi中的copy函数和pos函数
- DELPHI定义的条件编译的全部说明
- delphi 读写文本文件(函数比较全)
- Delphi xe10 FireDac 之 如何生成操作日志
- 让delphi程序不受WINDOWS操作系统日期格式的影响
- Delphi判断文件是否正在被使用
- Delphi之websocket及socket.io开源组件
- 在 Delphi 中判断一个字符是中文的方法
- delphi excel插件重复新创工程
- 用DELPHI压缩ACCESS数据库
- delphi中日期和时间接口、运算接口
- Delphi获取Excel文档创建、修改、访问的日期和时间