Basic to Creating a DLL on Delphi
unknow - Date: ?
unknow - Date: ?
[SHOWTOGROUPS=4,20]
Delphi allows not only to create applications but also DLL files. A DLL file (short for dynamic link library) is a special kind of executable that cannot be started by itself, but exports procedures and functions (called "Entry Points") for other executables to call.
Creating a DLL file with Delphi
Creating a DLL file with Delphi consists of two steps:
1. Creating a library project
2. Exporting some functions from that library
Creating a library project
Delphi has got a special kind of project for a DLL file. To create one you use the File -> New -> Other menu entry to open a dialog where you can select the project type you want to create. There is one called "DLL Wizard"; that's the one we need here.

Select it and press OK and Delphi will automatically create a new DLL file project for you. The source code looks like this:
There is also a comment about using strings and ShareMem, but we can ignore that for now.
At first glance, this looks like the project source for a normal console program. There is only one difference: It starts with the keyword library rather than program.
Another difference is how it is listed in the project explorer. Where an application has got the extension ".exe" a library has got ".dll". So the new project is called "Project1.dll" rather than the usual "Project1.exe".
Go ahead and save the new project giving it a name like "MyFirstLibrary".
Exporting functions from a library
Now let's export some function from our new library. Let's start with something very simple that does not involve complex data structures or a GUI: A function for adding two integers and returning their sum:
Yes, that's not very exciting, is it? But it is a function that can be called from any Windows program that wants to do it.
Side note about calling conventions
Now, this is getting rather technical and confusing: Programming languages have evolved over the years to pass parameters and return values from and to function calls in different ways.
Originally that was only inside one language and using only one compiler so the programmer just wrote the code and the compiler took care of how the functions were called.
Then came DLL files and all of a sudden code written in one language using one compiler could be called from other code written in a different language or only using a different compiler.
It turned out that this didn't work, so the compiler makers came up with the notion of calling conventions. We will use one called StdCall here since it allows for most other languages and compilers to call our DLL file. (One particular example is Visual Basic (from before .NET), which supports only the StdCall calling convention.)
Exporting the function
So let's add a calling convention to our function:
Of course in a more complex DLL file we would not add all the code to the project source but to units, but we want to keep things simple here. So this is what our source code looks for now:
Building the DLL file
To create a DLL file from this library, sourcecode is as simple as creating an executable for a normal Delphi application: Just build it. You end up with a file called MyFirstLibrary.dll.
Looking inside the DLL file
Now, a DLL file is a binary file and if you e.g. load it into an editor, you will see lots of gibberish. But there are tools that can extract some more information about it, like Dependency Walker or the PE-Information Wizard in GExperts. If you open your DLL file with this wizard, it will tell you the following:

(And lots of other things that we don't currently care about.)
So it worked, you now have got a DLL file that exports the function AddIntegers.
Using the DLL file
So, now how do we call that DLL file? Simple: You just tell the compiler what DLL file to load and which function to call with which parameters.
Creating a test project
Close the library project (File -> Close All) and create a new console application (File -> New -> Other + Select "Console Application").
You will end up with an empty console application project like this:
Save it as "MyLibraryTest"
Calling the DLL function
Add the following function declaration:
function AddIntegers(_a, _b: integer): integer; stdcall; external 'MyFirstLibrary.dll';
And call that function from within the main program:
Now your project should look like this:
Build your project and run it. You should now see a console window like this:

Isn't that exciting? You just wrote your first DLL file and called it from a test program! ;-)
[/SHOWTOGROUPS]
Delphi allows not only to create applications but also DLL files. A DLL file (short for dynamic link library) is a special kind of executable that cannot be started by itself, but exports procedures and functions (called "Entry Points") for other executables to call.
Creating a DLL file with Delphi
Creating a DLL file with Delphi consists of two steps:
1. Creating a library project
2. Exporting some functions from that library
Creating a library project
Delphi has got a special kind of project for a DLL file. To create one you use the File -> New -> Other menu entry to open a dialog where you can select the project type you want to create. There is one called "DLL Wizard"; that's the one we need here.
Select it and press OK and Delphi will automatically create a new DLL file project for you. The source code looks like this:
Код:
library Project1;
uses
SysUtils,
Classes;
{$R *.res}
begin
end.
At first glance, this looks like the project source for a normal console program. There is only one difference: It starts with the keyword library rather than program.
Another difference is how it is listed in the project explorer. Where an application has got the extension ".exe" a library has got ".dll". So the new project is called "Project1.dll" rather than the usual "Project1.exe".
Go ahead and save the new project giving it a name like "MyFirstLibrary".
Exporting functions from a library
Now let's export some function from our new library. Let's start with something very simple that does not involve complex data structures or a GUI: A function for adding two integers and returning their sum:
Код:
function AddIntegers(const _a, _b: integer): integer;
begin
Result := _a + _b;
end;
Side note about calling conventions
Now, this is getting rather technical and confusing: Programming languages have evolved over the years to pass parameters and return values from and to function calls in different ways.
Originally that was only inside one language and using only one compiler so the programmer just wrote the code and the compiler took care of how the functions were called.
Then came DLL files and all of a sudden code written in one language using one compiler could be called from other code written in a different language or only using a different compiler.
It turned out that this didn't work, so the compiler makers came up with the notion of calling conventions. We will use one called StdCall here since it allows for most other languages and compilers to call our DLL file. (One particular example is Visual Basic (from before .NET), which supports only the StdCall calling convention.)
Exporting the function
So let's add a calling convention to our function:
Код:
function AddIntegers(const _a, _b: integer): integer; stdcall;
begin
Result := _a + _b;
end;
And export it:
exports
AddIntegers;
Код:
library MyFirstLibrary;
uses
SysUtils,
Classes;
{$R *.res}
function AddIntegers(const _a, _b: integer): integer; stdcall;
begin
Result := _a + _b;
end;
exports
AddIntegers;
begin
end.
To create a DLL file from this library, sourcecode is as simple as creating an executable for a normal Delphi application: Just build it. You end up with a file called MyFirstLibrary.dll.
Looking inside the DLL file
Now, a DLL file is a binary file and if you e.g. load it into an editor, you will see lots of gibberish. But there are tools that can extract some more information about it, like Dependency Walker or the PE-Information Wizard in GExperts. If you open your DLL file with this wizard, it will tell you the following:
(And lots of other things that we don't currently care about.)
So it worked, you now have got a DLL file that exports the function AddIntegers.
Using the DLL file
So, now how do we call that DLL file? Simple: You just tell the compiler what DLL file to load and which function to call with which parameters.
Creating a test project
Close the library project (File -> Close All) and create a new console application (File -> New -> Other + Select "Console Application").
You will end up with an empty console application project like this:
Код:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
{ TODO -oUser -cConsole Main : Insert code here }
end.
Calling the DLL function
Add the following function declaration:
function AddIntegers(_a, _b: integer): integer; stdcall; external 'MyFirstLibrary.dll';
And call that function from within the main program:
Код:
WriteLn(AddIntegers(1, 2));
Write('Press Enter'); ReadLn;
Код:
program MyLibraryTest;
{$APPTYPE CONSOLE}
uses
SysUtils;
function AddIntegers(_a, _b: integer): integer; stdcall; external 'MyFirstLibrary.dll';
begin
WriteLn(AddIntegers(1, 2));
Write('Press Enter'); ReadLn;
end.

Isn't that exciting? You just wrote your first DLL file and called it from a test program! ;-)
[/SHOWTOGROUPS]