Delphi to write an instance of a DLL for C # calls

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.