Writing DLLs in Delphi

Setting up the Delphi Environment

I think it is important the DLLs I write can be called from any language. This basically means following the C style. (This is most important with strings, see later). Therefore I use Delphi as a prototyping tool for VB so I can do all the development and testing in Delphi. When I create the DLL and test from VB, I know the code is working OK.

Step 1: I create a Delphi project in the normal way, say rg.dpr. In this project I create a form and add buttons. Under the button.click event I put code to test the procedure being created. All the code to be included in the DLL is put into other units. Therefore, the smallest application has four files.

rg.dpr.the project file
rgtest.dfmthe form code
rgtest.pasthe unit file with the button click code
rg2.pasthe unit file(s) with the DLL code

I then create another project, say rgdll.dpr, using the following method:

  1. Close all existing projects and units.
  2. Create a new project with menu File/New Project
  3. Remove form1 using menu View/Project Manager,delete unit
  4. Add units containing the DLL code using menu File/Add File
  5. Display project file using menu View/Project Source and then edit the project file to set declarations for creating the DLL.

The new project file, with the changes shown, is below:

  library Rgdll;              {*1.program changed to library}
  uses                        {*2.removed Forms }
    Rg2 in 'RG2.PAS';         {$R *.RES}
  exports                     {*3.add exports clause and the}
      ChangeInt,              {functions and procedures to export}
      ChangeSingle;
  begin                       {*4.remove code from main block}
  end.

Therefore, the smallest DLL project has 2 files:

  rgdll.dpr.   the project file
  rg2.pas      the unit file(s) with DLL code


Image of arrow to previous article Image of arrow to next article

Image of line
[HOME] [TABLE OF CONTENTS] [SEARCH]