DLL(100分)

  • 主题发起人 主题发起人 happy_sue
  • 开始时间 开始时间
H

happy_sue

Unregistered / Unconfirmed
GUEST, unregistred user!
How to use DLL in Delphi
 
你这个问题问得是不是太……了?要不你用“DLL”检索一下以前答案吧。
 
哈,小菜来捡分罗。^_^
这是用来调用DLL的那个单元
/////////////////////////
unit DoMain;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
HInst: THandle;
FPointer: TFarProc;
MyFunct: TIntFunction;
begin
HInst := LoadLibrary('FormDll.dll');
if HInst>0 then
try
FPointer := GetProcAddress(HInst,'RunForm');
if FPointer<>nil then
begin
MyFunct := TIntFunction(FPointer);
MyFunct;
end
else ShowMessage('动态链接库函数未找到!');
finally
FreeLibrary(HInst);
end
else ShowMessage('动态链接库未找到');
end;

end.

////////////////////////////////////////
这是DLL那个单元:
library Output1;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
Windows,
SysUtils;

{$R *.RES}

function PrintDate: ShortString;
begin
Result := DateToStr(Date);
end;

exports
PrintDate;
end.

 
刚才说错了,下面那一段是项目文件,不是单元。
再看这个:
下面这具程序是把一个普通的窗体项目做成了一个DLL(如果不做成DLL它就是一个执行
文件),调用它的程序只需要调用“RunForm“这个过程就可以了
////////////////这是项目文件//////////////////
Library FormDll;

uses
Forms,
DF1 in 'DF1.pas' {Form1};

procedure RunForm;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;

exports
RunForm;
end.

////////////////这是项目的窗体单元://////////////////////
unit DF1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
MessageBox(Handle,PChar('Good by, '+Edit1.Text),'DLL',MB_ICONINFORMATION+MB_OK);
Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Label2.Caption := FormatDateTime(LongDateFormat,Date);
Label2.Left := (Form1.Width div 2)-(Label2.Width div 2);
end;

end.
 
我觉得使用DLL遇到最多麻烦的不是使用自己的DLL,而是第三方的DLL!
使用第三方DLL时,你的注意很多哦。
弄明白人家DLL提供了那些函数过程,那些入口参数。最正要的是
这些参数的调用顺序(C或Pascal)。决定用何种调用方式(隐式或显式)。
哎,等你能用Windows的Cards.dll就差不多了。
 
不知是想送分还是不想给分
 
多人接受答案了。
 
后退
顶部