Sunday, April 20, 2008

Introduction to Dynamic Link Libraries DLLs.

Once there was a discussion between me and my friend as why DLL's are required and how to do that in Delphi. So, I thought of demonstrating a very simple program to show how simple it is to create a DLL in delphi.

Dynamic link library or DLL is a collection of routines (i.e. small programs) that can be called by other applications and DLLs.

Dynamic link libraries or DLLs are pieces of sharable codes that various other applications can use. These DLLs contain common code that may be required by different applications to perform similar operations. Thus allowing a single copy of routines to be used by many applications at the same time.

Another advantage of DLLs is that we can safely use routines created in other development environments like VB, C++ to be used in delphi or vice-versa.

Now you must be thinking as how to create a DLL in delphi. Is it really difficult or is it simple?

Well dear developer no need to worry. Its pretty simple in delphi.

Here I will try to create a simple delphi application using a new DLL -
This example will try to do the following for us -

First we will create a DLL that will have methods in it -

  • First will be a procedure and will display a welcome message.
  • Second procedure will display a message dialog and will tell us when summation of numbers will start.
  • Third procedure will also display a message dialog and will tell us when the summation is over and finally,
  • Fourth will be a function and will actually do summation of two numbers for us and return the result back to our calling program.
The two methods "Welcome" and "sumnumbers" will be called by our delphi application, remaining two are called from inside the sumnumbers function and are for our dll's internal use.

So all set in our mind then lets create our DLL now ->

First open Delphi and select File -> New -> DLL to create a DLL template for us. Then add Dialogs unit to uses portion of the DLL in case it is not already present (This is because I want to show a dialog box in our DLL to highlight the progress). Now, Save the project as MyLibrary.

Well thats not all done, we have to do some more...
But first observe the first line of our DLL. Here , we see that the keyword used is library and not program as in case of form based delphi applications. Well this is because this keyword tells that this is a DLL file and not other delphi application. So, now add the following code to the file -

library MyFirstDLL;

{ 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,
Dialogs;

{$R *.RES}

procedure Welcome(); export;
begin
ShowMessage('Welcome to MyLibrary DLL. You are about to view a DLL demo.');
end;

procedure firstmsg();
begin
ShowMessage('Summation is about to start. Please wait');
end;

procedure lastmsg();
begin
ShowMessage('Summation complete. Thanks for using DLL');
end;

// This function will sum two numbers and display result
function sumnumbers(first, second : integer): integer; export;
var
sum : integer;
begin
firstmsg();
sum := first + second;
lastmsg();
Result := sum; // Sending the output back to calling application
end;

exports Welcome, sumnumbers;

begin
end.
Save the project as MyFirstDll. Now compile the DLL project by pressing Ctrl + F9. On compilation a dll file will be created for us in the destination folder (where we saved the project named MyFirstDLL.). So, our dll file is created and the name is - MyFirstDLL.dll.

Now close the project and open a new delphi application by selecting File -> New Application from the main menu. Now add three edit boxes and a button on the form (Hey, I leave the GUI design for you.. :).. ). Now add the following code to your application and save the application (in the same location where we saved our MyFirstDLL.dll application) and the unit as FirstDLLPrj and FirstDLL respectively.

unit FirstDLL;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
procedure Welcome; external 'MyFirstDLL.dll'
function sumnumbers (a, b :integer) : integer; external 'MyFirstDll.dll'

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Welcome;
Edit1.text := '';
Edit2.text := '';
Edit3.text := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.Text := inttostr (sumnumbers(strtoint(Edit1.Text), strtoint(Edit2.Text)));
end;

end.

Now all done then we are ready to compile our application. Hey, what are you waiting for press Ctrl F9 and now run the application and Bingo here is our first DLL application created.

Simple isn't it?

Hey, I know what you are thinking now. You must be thinking, "hey hello!! what happened in the above code actually?"

So no need to panic, I will explain what has actually happened above.


First while creating the DLL we did the following - we created two methods (procedure Welcome and function sumnumbers) that we wanted to be used by other applications (These are the common routines that are exported.).

Now to make a procedure or function external i.e. to be used by other applications we have to tag them with export keyword in there definition (see use of export keyword against the procedure Welcome and also against function sumnumbers in code above). The keyword export will tell that the following procedure or function is exportable i.e. could be referenced by outside applications.

The other two procedures firstmsg and lastmsg are for internal purpose (i.e. within our dll and cannot be refered by outside applications) and hence are not tagged with the keywords export.

Finally, to let the compiler know which all are the functions and procedures that are being exported by the DLL we will define the list with exports keyword (see our dll code above) -

exports Welcome, sumnumbers;So, that's all what was required to write a DLL code.

Now coming to the calling application that will actually use this dll and its routines.

To use the DLL routines in our code we have to declare the procedures and functions so that the compiler could find the entry points to these routines. This we did by writing the following code in our calling application -

procedure Welcome; external 'MyFirstDLL.dll'
function sumnumbers (a, b :integer) : integer; external 'MyFirstDll.dll'Above statements have the following portions, (carefully observe these) - first is the name of the procedure as defined in the DLL, then the keyword external telling that the procedure/function is an external function and finally defining the dll file name where the procedure is actually located.

We have used the name of the dll file in our code as the dll was located in the same folder where our calling program is located otherwise we would have to specify the complete path name to access the dll because if the dll won't be located in the same folder we will get an entry point error while running the application.

The entry point error tells that the compiler is not able to find the function or procedure specified in the calling program and so is not able to attach it to our application and execute the routine.

Rest we come to calling and invoking the procedure or function, this is done in the same way as is done for normal procedures and functions in a delphi application. Here it could be noted that we didn't use any special method for invoking the procedures in our code because the dll functions and procedures are now a part of our application.

So, thats it for now. We have finally created our DLL. Now, Enjoy writing more DLL's... My job is done... Astalavista till next article.

No comments: