First delphi code (not a Hello World!) - Basics of dealing custom DLL
First delphi code (not a Hello World!) - Basics of dealing custom DLL

I have been thinking about learning a new programming language for a while. Finally there is an opportunity to get my hands dirty with delphi. Why delphi? Bcoz, it's been there for a while and there is a big community and industry that use delphi. In fact, while searching for learning materials I found that, delphi is sort of native language to the Russian nerds (no offense but with due respect). Delphi is a strongly typed and pascal based programming language which is why I think it would be quite easy to learn as I already know a little bit of C#. And still if you are not satisfied with my reasoning then, well yeah...I need to learn it bcoz at work I need it. Happy now?

So, as I said that, I already know C# so obviously I am and will be looking for similarities while learning delphi and decided to write my first code in delphi about something interesting rather than just printing "Hello world!" or "Hey buddy!". So, I decided to write about handling custom DLLs in delphi. In this regard, I must mention that I will be using Delphi 2007 for this post and may be for the future ones too. 

What is DLL:

Normally, a dll is a collection of code blocks that typically allows you to do something. Dll allows you to share resources in your projects and provides some abstractions. 

DLL in delphi:

To start with first we will create a normal console application and name it "DealingDLL". In our project manager window we can see that "DealingDLL.exe" is created. This is the bare bone of an application that compiles and runs. Nothing really happens at this stage even if we try to run our application. We run application in delphi by pressing F9 or by clicking the small green play button.
As a next step, we will create another project under the same project group of type "DLLWizard" and call it "MyDLL". Now in our project manager window we can see that "MyDLL.dll" is created. We know its a dll because it has an extension of dll and if we go inside of the dll, we will see at the very top it says "library MyDLL;". If we compare it with the "DealingDLL.exe", we will see that inside the "DealingDLL.exe" it says "program DealingDLL;" at the very top. Now, how do we start using the "MyDLL" inside our console application. In C# we can refer to a dll or assembly by using the "using" keyword but here the story will be bit different. 

Inside the DLL:

In delphi, a dll file starts with the "library" keyword and before the main "begin and end." we will have procedures and functions as we need. In this regard a procedure in delphi means (from C# point of view) a void method and a function is a method that is not void. We will have the following two functions in our dll file

procedure SayHelloToName(name: string); stdcall; export;
    Writeln('Hello ' + name);
function ReturnTitle(gender:string; isMarried: boolean): string; stdcall; export;
     if gender = 'male' then
          Result := 'Mr.'
     else if (gender = 'female') and (isMarried = true) then
              Result := 'Mrs.';
     else  if (gender = 'female') and (isMarried = false) then
              Result := 'Ms.';
              Result := '[please specify your gender as male OR female] ';

Notice that, the syntax of a procedure and function is almost the same except the initial keywords and the return type in a function. Yes, the ":string" part at the end of the function "ReturnTitle" is the return type of the function. 

Also, notice the use of "stdcall;" and "export;" keywords. By "stdcall" we are making it possible for application written in any other language to use this dll and by "export" we are saying that this procedure or function is exportable. I said 'exportable' meaning the trading is not done yet.

The trading is done (from the dll's side) after we have used the "exports" keyword like below 


Meaning, our above mentioned "SayHelloToName" procedure and "ReturnTitle" function are now accessible from any application that wants to use our dll. I know, the "ReturnTitle" function is quite verbose but hey, I am learning delphi, remember? 

Inside the application:

Now, time for action. Inside our "DealingDLL.exe" we refer to the procedures or functions from a custom dll like the following

procedure SayHelloToName(name: string); stdcall; external 'MyDLL.dll';
function ReturnTitle(gender: string; isMarried: boolean): string; stdcall; external

Two things to be noticed here with care. first one is, there is no ";" at the end of "external" in our calls inside the "DealingDLL.exe" and the last bit where we are mentioning to path where the dll is located (the absolute path). In our case we defined that the dll can be found at the same location of the application root. We can do this by right click on the "MyDLL" project > Options... > Diretories/Conditionals > Output Directory and select the folder where we created our "DealingDLL" project.

Finally we can use those produre and functions inside the main "begin and end." block like below and press that green play button to see our very first delphi application in action.

Writeln('Welcome to my console...'); //simple write line
SayHelloToName(ReturnTitle('female',true)+ 'Jahan');
Readln; //simple read line to prevent the app from closing immediately

Happy coding!!!

April 24, 2015
Jahan Sarwar