Solved

need Dll example

Posted on 2001-08-09
4
185 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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