Solved

need Dll example

Posted on 2001-08-09
4
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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