谁有调用DLL文件的简单例子?

pclogic 2003-10-27 08:28:58
最简单的就行了!
...全文
43 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fhuibo 2003-10-27
  • 打赏
  • 举报
回复
study
Northwindrocker 2003-10-27
  • 打赏
  • 举报
回复
我给你一个!是数美分的
下面的是DLL单元
library pennieslib;
{$define pennieslib}

{ 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
SysUtils,
Classes,
//penniesint in 'penniesint.pas';

penniesint;
function penniestocoins(Totpennies:word;coinsrec:pcoinsrec):word;stdcall;


begin
result := totpennies;
with coinsrec^ do
begin
quarters := totpennies div 25;
totpennies := totpennies - quarters*25;
dimes := totpennies div 10;
totpennies := totpennies -dimes * 10;
nickels := totpennies div 5 ;
totpennies := totpennies - nickels*5;
pennies := totpennies;
end;
end;
exports
penniestocoins;


end.
就是定义了一个函数如何把美分换成硬币的个数
下面是这个DLL的接口单元
unit penniesint;

interface
type
pcoinsrec = ^tcoinsrec;
tcoinsrec = record
quarters,
dimes,
nickels,
pennies:word;
end;
{$ifndef pennieslib}
function penniestocoins(totpennies:word;coinsrec:pcoinsrec):word;stdcall;
{$endif}

implementation
{$ifndef pennieslib}
function penniestocoins;external 'pennieslib.dll' name 'penniestocoins';
{$endif}
end.

下面是在一个工程中调用这个DLL
{ Copyright ?2001 Delphi 6 Developer's Guide Xavier Pacheco
and Steve Teixeira }


unit MainFrm;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Mask;

type

TMainForm = class(TForm)
lblTotal: TLabel;
lblQlbl: TLabel;
lblDlbl: TLabel;
lblNlbl: TLabel;
lblPlbl: TLabel;
lblQuarters: TLabel;
lblDimes: TLabel;
lblNickels: TLabel;
lblPennies: TLabel;
btnMakeChange: TButton;
meTotalPennies: TMaskEdit;
procedure btnMakeChangeClick(Sender: TObject);
end;

var
MainForm: TMainForm;

implementation
uses PenniesInt; // Use an interface unit

{$R *.DFM}

procedure TMainForm.btnMakeChangeClick(Sender: TObject);
var
CoinsRec: TCoinsRec;
TotPennies: word;
begin
{ Call the DLL function to determine the minimum coins required
for the amount of pennies specified. }
TotPennies := PenniesToCoins(StrToInt(meTotalPennies.Text), @CoinsRec);
with CoinsRec do
begin
{ Now display the coin information }
lblQuarters.Caption := IntToStr(Quarters);
lblDimes.Caption := IntToStr(Dimes);
lblNickels.Caption := IntToStr(Nickels);
lblPennies.Caption := IntToStr(Pennies);
end
end;

end.



hch_45 2003-10-27
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender: TObject);
const libName= 'a.dll';
var
libHandle : THandle;

A : function(stFileName : PChar): DWORD; stdcall;
begin
libHandle := LoadLibrary(libName);
if libHandle <> 0 then
begin
try
HCHFilterDownload := GetProcAddress(libHandle, 'a');

ShowMessage(IntToStr(A('C:\notepad.exe')));
except
on e:Exception do
ShowMessage(e.Message);
end;

FreeLibrary(libHandle);

end;
end;
太空11 2003-10-27
  • 打赏
  • 举报
回复
Dll 的动态调用
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
thellodll=procedure(myform:tform);
//end;

var
Form1: TForm1;
dllinstance:thandle;
pfunc:tfarproc;

implementation

//uses Unit1;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
thellodll=procedure(myform:tform);
var
dllinstance:thandle;
pfunc:tfarproc;
begin
dllinstance:=loadlibrary('d:\aaa\project1.dll');
if dllinstance=0 then
begin
showmessage('can not load dll');
exit
end;
pfunc:=getprocaddress(dllinstance,'hellodll');
if pfunc<>nil then
thellodll(pfunc)(form1)
else
showmessage('can not find the function');
freelibrary(dllinstance);

end;

end.
////////////////////////////////////////////////////////////
library jiemian;


uses
SysUtils,
forms,
Classes,
Unit1 in 'Unit1.pas' {Form1};


{$R *.res}
function showform:integer;stdcall;
var
form:tform1;
begin
form:=tform1.Create(application);
result:=form.ShowModal;
form.Free;
end;
exports
showform;

begin
end.

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧