DLLs written in Delphi:
LibraryTestdll;{Important Note about DLL memory Management:sharemem must is the first unit in your library ' s USES clause and your P Roject ' s (select Project-view Source) USES clause If your DLL exports any procedures or functions this pass strings as P Arameters or function results. This is dll--even in those and nested, applies to all strings passed to and from your records classes. SHAREMEM is the interface unit to the BORLNDMM. DLL shared Memory manager, which must is deployed along with your DLL. To avoid using BORLNDMM. DLL, pass string information using PChar or shortstring parameters. }usessysutils, Classes;functionADD (X,y:integer): Integer;stdcall;beginResult:=x+Y;End;functionTxx (x,y:double):D ouble;stdcall ;beginResult:=x+y+y+x;End;functionEncryvncpassstrhex (Str:pchar): PChar;stdcall;vartempresult:string;beginTempresult:=widechartostring (str); Result:=PChar (tempresult);End;{$R *.res}exportsADD, Encryvncpassstrhex, Txx;beginEnd.
There are three output functions involved: One output variable is an integer, one is a floating-point number, and the other is a string.
Call this DLL in C #:
usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;namespaceusedll{/// <summary> ///Description of MainForm. /// </summary> Public classUsedll {[DllImport ("TestDLL.DLL", entrypoint="ADD", charset= charset.auto,callingconvention=Callingconvention.stdcall)] Public Static extern intAddintXinty); [DllImport ("TestDLL.DLL", entrypoint="Txx", charset= charset.auto,callingconvention=Callingconvention.stdcall)] Public Static extern DoublePP (DoubleXDoubley); [DllImport ("TestDLL.DLL", entrypoint="Encryvncpassstrhex", CharSet=CharSet.Auto, callingconvention =Callingconvention.stdcall)] Public Static extern stringEncryvncpassstrhex (stringStr); } Public Partial classMainform:form { PublicMainForm () {// //The InitializeComponent () call are required for Windows Forms Designer support. //InitializeComponent (); // //Todo:add Constructor code after the InitializeComponent () call. // } voidButton1Click (Objectsender, EventArgs e) { intx=Ten; inty= -; intz=usedll.add (x, y); This. textbox1.text=z.tostring (); Doubleoo=0.123; Doublepp=0.254; This. button1. text=usedll.pp (OO,PP). ToString (); This. Text =usedll.encryvncpassstrhex ("I am xxx"); } }}
Note: "using System.Runtime.InteropServices" must be added to the using section of C #.
The result of the program operation is:
This procedure is adopted in delphi2010 and SharpDevelop.
Delphi to write an instance of a DLL for C # calls