How to write and call DLL's within Delphi

In this example I:

  1. 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).
  2. 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

  1. Start a new DLL project in Delphi (Click File->New, select DLL).
  2. Save the project as delhpdll
  3. Fill in the code in the library
  4. 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:

  1. Start up a new "main" application project.
  2. Make whatever GUI controls you need to test the DLL
  3. Fill in the source code for interfacing the DLL.
  4. Go, fly, run.