Creating the DLL

The first step in this project is to create the DLL that will contain the form. To do this follow these steps:

1. Start a new project using File – New DLL You will now have a basic Dll template You now need to create a form to add to this project, do File- New Form
2. Next we need to add the form into the project, so save the form with a relevant name (I used DllForm) and add it into the project using File – Add To Project… in the displayed dialog box selected your newly saved form (DllForm.pas)

We now have a simple DLL which contains a form but at present there is no to call the DLL or display the form. The code needed to do this will have to be written manually, as will be explained in the next section.
Writing the code in the DLL

In this example I will provide two methods (one procedure and one function) for displaying the form, one to display it using the Show method and one to display it using the ShowModal method.

The first procedure to just show the Form is written like this:

procedure ShowDllForm;stdcall;
begin
  frmDllForm :=TfrmDllForm.Create(nil);
  frmDllForm.Show;
end;

To show the form modally is slightly different as we need to return the modal result. The code to do this is as follows:

function ShowDllFormModal:integer;stdcall;
begin
  frmDllForm :=TfrmDllForm.Create(nil);
  Result := frmDllForm.ShowModal;
end;

The only difference between this code and the previous example is that we are using a function instead of a procedure so that we can return the modal result of the form, and we are calling the ShowModal method instead of Show.

One thing you have noticed in these examples is that we are creating the form but never destroying it, which would result in our application wasting resources (leaking memory). The easiest way to destroy the Form is to do this by using its OnClose event, and setting its TCloseAction to caFree (i.e. all memory associated with form is freed when the form is closed). This done using the following code in the OnClose event of the form:

procedure TfrmDllForm.FormClose(Sender: TObject;
                                var Action: TCloseAction);
begin
  Action := caFree;
end;

Finally in the DLL we need to export the functions so that they can be called by our application. This is done by adding the following code to the DPR file:

Exports
    ShowDllForm,
    ShowDllFormModal;

We have now finished writing the DLL so just need to build it and get on with writing the application that will call it.
Creating the Application

To create the application we need to perform the following steps:

1. Start a new project using File – New Application Next we need to add two buttons to the main form, one to show the form in DLL normally and one to show it modally And two buttons to the form by selecting them from the component palette and placing them on the form.
2. Change the captions on the buttons to say what they will do and resize them if necessary.

We now have a simple application that does nothing so all that needs doing now is to add the code for the buttons to perform their required functions.
Writing the code for the Application

The first stage in writing the code is to write the declarations for the procedure and function that we will be calling in the DLL. This is done before the implementation section of the pas file that the function will be called in, like this:

procedure ShowForm;stdcall;
    external 'Project1dll.dll' name 'ShowDllForm';
   
function ShowFormModal:integer;stdcall;
    external 'Project1dll.dll' name 'ShowDllFormModal';