Solved

need Dll example

Posted on 2001-08-09
4
181 Views
Last Modified: 2010-04-06
Hello,

i need some example how to create DLL and call DLL from delphi.


0
Comment
Question by:asi
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
mnasman earned 30 total points
Comment Utility
Hello

  This is very simple and easy aricle will help you to create and use dll with delphi
http://delphi.about.com/library/weekly/aa041100a.htm

also look at these pages
http://www.delphi-dolphin.com/chapter.php?chapter=21
http://www.undu.com/dn951010/00000008.htm


Best regards
Mohammed Nasman
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
heres a quick example with a simple function in dll:


the dll saved and built as "theint.dpr" :


library theint;

uses
 SysUtils,Classes,Windows;

function Showint : integer ;far;
begin
 Result := 50;
end;

exports

Showint;

begin
end.



and the calling unit:


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls;

type
 TForm1 = class(TForm)
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
type
  TShowint = function : integer; stdcall;
 var
 Form1: TForm1;

 DLLHandle : THandle;
 ShowintA : TShowint;

 implementation

{$R *.DFM}

procedure LoadDLLFunction;
begin
 Dllhandle := LoadLibrary( 'theint.dll' );
 try
   if Dllhandle <> 0 then
   begin
     @ShowintA := GetProcAddress(DllHandle, 'Showint' );
     if @ShowintA <> nil then
       ShowMessage(IntToStr(ShowintA))
     else
       showmessage('error linking ShowintA');
   end
   else
     showmessage('error linking dll');
 finally
   FreeLibrary( Dllhandle );
 end;
end;



procedure FreeDLLFunction;
begin
   if DLLHandle <> 0 then
         FreeLibrary(DLLHandle);
end;

Procedure TForm1.Button1Click(Sender: TObject);
begin
LoadDLLFunction;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeDLLFunction;
end;

initialization
begin
DLLHandle := 0;
end;

end.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
:-(   You have to use the same calling convention in both the dll and the application. In Barry's example the exported function in the dll is declared as:

function Showint : integer ;far;

In the application it's declared as:

type
 TShowint = function : integer; stdcall;

Do you see the difference? "far" is obsolete. It has no meaning in 32bit programming. But "stdcall" is a decisive calling convention. Barry's example will run fine, because the result value is handled the same way in all calling conventions, but as soon as you extend Barry's example with one parameter, it will crash, because the calling conventions are different...

Regards, Madshi.

P.S: I can also offer two examples about using DLLs in Delphi. The sense of those example is to demonstrate the usage of my packages, not to explain how DLLs work. But maybe it's of some use for you, nevertheless:
http://help.madshi.net/Data/madExceptDemo.htm
http://help.madshi.net/Data/HookingNotepad.htm
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
yikes this is true and thats how bugs can happen :o)
certainly change that to stdcall.




0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now