Sunday, April 20, 2008

Introduction to Dynamic Link Libraries DLLs.

Once there was a discussion between me and my friend as why DLL's are required and how to do that in Delphi. So, I thought of demonstrating a very simple program to show how simple it is to create a DLL in delphi.

Dynamic link library or DLL is a collection of routines (i.e. small programs) that can be called by other applications and DLLs.

Dynamic link libraries or DLLs are pieces of sharable codes that various other applications can use. These DLLs contain common code that may be required by different applications to perform similar operations. Thus allowing a single copy of routines to be used by many applications at the same time.

Another advantage of DLLs is that we can safely use routines created in other development environments like VB, C++ to be used in delphi or vice-versa.

Now you must be thinking as how to create a DLL in delphi. Is it really difficult or is it simple?

Well dear developer no need to worry. Its pretty simple in delphi.

Here I will try to create a simple delphi application using a new DLL -
This example will try to do the following for us -

First we will create a DLL that will have methods in it -

  • First will be a procedure and will display a welcome message.
  • Second procedure will display a message dialog and will tell us when summation of numbers will start.
  • Third procedure will also display a message dialog and will tell us when the summation is over and finally,
  • Fourth will be a function and will actually do summation of two numbers for us and return the result back to our calling program.
The two methods "Welcome" and "sumnumbers" will be called by our delphi application, remaining two are called from inside the sumnumbers function and are for our dll's internal use.

So all set in our mind then lets create our DLL now ->

First open Delphi and select File -> New -> DLL to create a DLL template for us. Then add Dialogs unit to uses portion of the DLL in case it is not already present (This is because I want to show a dialog box in our DLL to highlight the progress). Now, Save the project as MyLibrary.

Well thats not all done, we have to do some more...

Sunday, April 13, 2008

Can Objects be assigned to a TStringList?

Well if someone asks that can we store an object in a TStringlist or should we use only TList for this purpose, the answer should be ----> Yes ofcourse!.

Now how to do that and where can I use such facility. Now first to answer why should we require this feature if at all. One place where we can use such a feature is when we are required to save or associate an object to a particular name, like saving a location name with the corresponding bitmap.

Now coming to how to do that in delphi. The answer is using the Objects property associated with TStringlist. Delphi gives the power of associating objects to tstringlist using this simple property.


Please see the below example to see how to do that -

Here in this example add a button to a simple delphi form. Now add two edit texts and add a TMemo component. Now write the following code on the button click event -

procedure TForm1.Button2Click(Sender: TObject);
var
mystringlist : tstringlist;
begin
Memo1.clear;
mystringlist := tstringlist.create();
mystringlist.Add(edit1.text);
mystringlist.Objects[0] := edit1;
mystringlist.Add(edit2.text);
mystringlist.objects[1] := edit2;
memo1.Lines.Add(TEdit(mystringlist.objects[0]).name);
memo1.Lines.Add(TEdit(mystringlist.objects[1]).name);
end;

Run the delphi program by pressing F9 key or navigating to "Run" Menu and clicking on "Run" Menu item.

Now click on the button on the form and you will see that memo displays two strings edit1 and edit2. Here the names have been derieved from the objects stored in the stringlist.

This was just a demonstration program and the power of such a feature can be easily utilized in performing complex tasks.