Solved

need Dll example

Posted on 2001-08-09
4
182 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:
Mohammed Nasman earned 30 total points
ID: 6368944
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
ID: 6368954
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
ID: 6369122
:-(   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
ID: 6369363
yikes this is true and thats how bugs can happen :o)
certainly change that to stdcall.




0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

10 Experts available now in Live!

Get 1:1 Help Now