Write a DLL in Delphi That Exports Functions and Procedures

Use Delphi to create a DLL that will allow you to update and change application functionality without having to redistribute your entire application.
by Harald Gill

LLs are a great way to simplify code reuse under Windows and are very easy to create using Delphi. By building up large libraries of objects and methods and storing them in DLLs, you will find your total workload reduced when creating new applications. DLLs also allow you to store code logically in compiled files, allowing you to update and change functionality as you see fit without having to redistribute your entire application. By specifying dynamic loading and using names to identify DLL functions, as long as you don't change the function signature, your application will be able to use your DLL.

Writing DLLs in Delphi is straightforward. You need only decide whether your DLL needs to be accessible from only Delphi programs or also from C++ or VB programs before getting started. The differences in accessibility are minor but they are important, so make up your mind first. If your DLL is going to be used by other languages, always use the stdcall calling convention, which Windows uses. If you know that your DLL will be used only by Delphi applications and libraries, then use the register calling convention. The register convention is faster but cannot be used by applications written in other languages. Generally, using stdcall is safer because you don't have to worry about your library in the future.

Build a Simple DLL
To start building your DLL, select File | New from Delphi's main menu. From the dialog, select the DLL project. This will open the code editor with the DLL main project file. Save this file as MyDll.dpr. Most instructional Delphi books will tell you to put your methods and/or structures right in this file and be done with it, but if you're really going to use DLLs, you should separate your files in some logical order. I'll demonstrate how in the following paragraphs.

Select File | New and select Unit from the New Items dialog. Save this file as uDll.pas, this is where you will place your code. For an easy start, let's write a function that passes a string into the DLL and returns a Boolean value. uDll.pas should look as follows:

Listing 1: Simple DLL Unit
unit uDLL;
interface
uses
Dialogs;

function DisplayMsg(s:String):Boolean;stdcall;

implementation

function DisplayMsg(s:String):Boolean;stdcall;
begin
ShowMessage(s);
Result := True;end;

end.

Remember to put the uses clause in exactly as shown. If you're missing the Dialogs unit, you'll get a compile time error.

 
Export the Function

In this Article
Introduction The DLL Interface
Export the Function Access the Application Object in Your DLL






FEATURE SOFTWARE:
SQLDC
Create SQL applications quickly and easily.
Buy Now!

FEATURE BOOK:
VBCommander
Speed app and component design time with this set of more than 20 add-in tools.
Buy Now!
Download the source code for this article!

10-Minute Solution: Create a Multiline Button Component

Get Help with Delphi

Database Development Zone




 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement