Tuesday, 11 May 2010

Creating DLL file using Delphi

There are two kinds of  executable file in Windows, EXE file and DLL file. DLL (Dynamic Link Library) is an executable file with some procedures or functions in it. These procedures or functions can be used by some applications at the same time. Because, once DLL is loaded to memory, every program or application can use it. The other advantage of using DLL is our application becomes modular. It will be easier to change the application by replacing the DLL.

To create a DLL file, choose File – New – DLL. And you’ll get some codes below :


library Project1;

{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }

uses

  SysUtils,

  Classes;

{$R *.RES}

begin

end.


You can delete the unnecessary comment after library clause and add exports clause before begin….end block.

One thing you have to concern about is that all variables in DLL are private. Your application and DLL can not use the variables at the same time. You have to send values of the variables and receive a result.

Now we’re going to insert some functions into DLL :


library area_volume;

uses

  SysUtils,

  Classes;

{$R *.RES}

function area(length, width : integer): integer; stdcall;

begin

 area := length*width;

end;

function volume(length, width, height : integer): integer; stdcall;

begin

 volume := length*width*height;

end;

exports

 area, volume;

begin

end.


You can download this source code here.

Now save the project as area_volume.dpr. Remember that the project name and the DLL name have to be the same, in this case we name it area_volume. Then build the DLL file by choosing Project – Build … And you’ll get area_volume.dll file. Then you can create an application using this DLL.


5 comments:

  1. sungguh ini tutorial yang sangat membantu kita dalam memahami fungsi-fungsi.

    moga sukses selalu kawan

    ReplyDelete
  2. Delphy, kok sulit c blajar nya. tolong sob, di posting mulai install , terus dasar2nya dan seterusnya..

    ReplyDelete
  3. @Pakde :thank you pakde :)
    @Ra'want : Its quite easy if you follow my tutorial in DELPHI(BEGINNER),see this blog archive. Installing Delphi is also simple, just like installing any other application (office ..etc) OK.. thank you for dropping by
    @kakara : OK... thanks

    ReplyDelete
    Replies
    1. s The best and a lot of secret drop-shipping suppliers in the world which will provide your products globally available for you directly to your customers doors. Also get the best drop-shipping manufacturer on your organization.

      Delete

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.