?
Solved

need Dll example

Posted on 2001-08-09
4
Medium Priority
?
188 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 90 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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