How to write and call DLL's within Delphi
In this example I:
- Create a Dynamically Linked Library (DLL) containing two functions, Min
and Max, which returns the largest of two integers (I know, this is not
rocket science).
- Create an application which calls the Max function from the DLL. This is
done in two ways: First, using the "external" declaration in
Delphi which is easy but rigid. Second, using the Win API which involves
writing more code, having less checks but more control.
You can download all files needed as a zip
archive. This includes all source, ressources, etc. for both the DLL project and
for the application project.
If you want to start from scratch, this is the procedure
- Start a new DLL project in Delphi (Click File->New, select DLL).
- Save the project as delhpdll
- Fill in the code in the library
- Build and Save the DLL project.
(If you are a command line kind of guy, then you can just execute "dcc32
delhpdll.dpr" from the command line ... that will give you the same DLL but
without the IDE stuff ...)
OK, then you need the application which should call the DLL:
- Start up a new "main" application project.
- Make whatever GUI controls you need to test the DLL
- Fill in the source code for
interfacing the DLL.
- Go, fly, run.