asi
asked on
need Dll example
Hello,
i need some example how to create DLL and call DLL from delphi.
i need some example how to create DLL and call DLL from delphi.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-( 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
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
yikes this is true and thats how bugs can happen :o)
certainly change that to stdcall.
certainly change that to stdcall.
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(Showi
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
begin
LoadDLLFunction;
end;
procedure TForm1.FormDestroy(Sender:
begin
FreeDLLFunction;
end;
initialization
begin
DLLHandle := 0;
end;
end.