各位高手:谁有用过delphi写DLL,怎么做呢?(100分)

  • 主题发起人 主题发起人 wishme1
  • 开始时间 开始时间
W

wishme1

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能给我举个简单的例子?
我想知道实现的步骤啊!
 
这个是DLL的代码:
library demo;

uses
SysUtils,
Classes;

{$R *.res}

function zhang(Z:integer):integer;stdcall;
begin
result:=5;
end;
function qi(Q:integer):integer;Stdcall;
begin
result:=2;
end;

exports
zhang,
qi;
begin
end.

这个是动态调用上面的DLL
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;

procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
PVFN = Function (para : Integer) : Integer;stdcall; //如果不加stdcall 则在释放时出现地址访问错误
TMyfunc =function(Z:integer):integer;stdcall;
var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ResultInt:integer;
MyFunc: TMyFunc;
Moudle: THandle;
begin
Moudle := Loadlibrary('c:/Demo.dll');
try
if moudle>0 then begin
@MyFunc := GetProcAddress(Moudle,'zhang');
if not(@MyFunc = nil) then
ResultInt:=MyFunc(3)
else
showmessage('error');
showmessage(intTostr(Resultint));
end
else
showmessage('error');
finally
FreeLibrary(Moudle);
end;
// closehandle(moudle);
end;
end.

这个是静态调用上面的DLL
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function zhang(Z:integer):integer;far external 'demo.dll';
function qi(Q:integer):integer;far external 'demo.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
zhangqi:integer;
begin
zhangqi:=zhang(2);
showmessage(inttostr(zhangqi));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
zhangqi:integer;
begin
zhangqi:=qi(3);
showmessage(inttostr(zhangqi));
end;

end.
 
在前函数加上STDCALL;
在DLL单元里导出该函数
Exports 函数(参数):返回值
 
library MinMax;
function Min(X, Y: Integer): Integer; stdcall;
begin
if X < Y then Min := X else Min := Y;
end;

function Max(X, Y: Integer): Integer; stdcall;
begin
if X > Y then Max := X else Max := Y;
end;

exports
Min,
Max;
begin
end.
===========================================
library Editors;
uses EdInit, EdInOut, EdFormat, EdPrint;
exports
InitEditors,
DoneEditors index 17 name Done,
InsertText name Insert,
DeleteSelection name Delete,
FormatSelection,
PrintSelection name Print,
...
SetErrorHandler;
begin
InitLibrary;
end.
 
Delphi5开发人员指南有详细说明,看一看就知道了!
 
多看书,别问这么低级的问题!
 
问的好, 看懂了
 
呵呵,对呀
这种问题,我实在不知道该从何答起
你先照着书作吧
有什么问题再请大家帮助不迟
 
每本程序书必讲内容!
而且书中的例子一看就懂,自己去试试!
我的个人主见:通过努力而解决不了的问题才上DFW!
 
file>new>other>选dll
 
个人觉得还是你自己亲手作过才会对它有更深的理解,也会发现更多的问题。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部