DLL:unit PenniesInt;
{ Interface routine for PENNIES.DLL }interface
type  { This record will hold the denominations after the conversion have
    been made }
  PCoinsRec = ^TCoinsRec;
  TCoinsRec = record
    Quarters,
    Dimes,
    Nickels,
    Pennies: word;
  end;{$IFNDEF PENNIESLIB}
{ Declare function with export keyword }function PenniesToCoins(TotPennies: word; CoinsRec: PCoinsRec): word; StdCall;
{$ENDIF}implementation{$IFNDEF PENNIESLIB}
{ Define the imported function }
function PenniesToCoins; external 'PENNIESLIB.DLL' name 'PenniesToCoins';
{$ENDIF}end.{
Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
}library PenniesLib;
{$DEFINE PENNIESLIB}
uses
  SysUtils,
  Classes,
  PenniesInt;function PenniesToCoins(TotPennies: word; CoinsRec: PCoinsRec): word; StdCall;
begin
  Result := TotPennies;  // Assign value to Result
  { Calculate the values for quarters, dimes, nickels, pennies }
  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;{ Export the function by name }
exports
  PenniesToCoins;
end.

解决方案 »

  1.   

    创建:NEW -- > Lib
      

  2.   

    调用:
    {
    Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
    }unit MainFrm;interfaceuses
      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.
      

  3.   

    chechy给的例子里调用程序中对dll的函数声明放错了位置,将这句话移下来:
    function PenniesToCoins; external 'PENNIESLIB.DLL' name 'PenniesToCoins';
    这里给一个动态调用dll里的一个函数的简单例子:
    dll:
    library testdll;uses
      SysUtils ,Classes;function Instr(a,b:Integer):Integer;stdcall; 
    begin
      Result := a+b;
    end;exports
      Instr Name 'MyInstr';begin
    end.
     
    调用:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
        Dialogs, StdCtrls;type
      TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
      { Private declarations }
    public
      { Public declarations }
    end;var
      Form1: TForm1;implementation{$R *.dfm}
    type
      FMyInstr = function(a,b:Integer):Integer;stdcall;procedure TForm1.Button1Click(Sender: TObject);
    var
      hm: HMODULE;
      My_Instr: Pointer;
      order:Integer;
    begin
      New(My_Instr);
      hm := LoadLibrary(PChar('D:\testdll.dll'));
      if hm > 32 then
         begin
         My_Instr := GetProcAddress(hm, PChar('MyInstr'));
         order := FMyInstr(My_Instr)(1, 4);
         showmessage(IntToStr(order));
         end
      else
         ShowMessage('Loading error!');
      FreeLibrary(hm);
    end;end.
      

  4.   

    没有定义错阿,有条件编译指令跟着
    如果DLL工程使用这个单元,定义PENNIESLIB,不编译
    普通工程使用,则编译,一个公共的接口单元
    {$IFNDEF PENNIESLIB}
    { Declare function with export keyword }function PenniesToCoins(TotPennies: word; CoinsRec: PCoinsRec): word; StdCall;
    {$ENDIF}implementation{$IFNDEF PENNIESLIB}
    { Define the imported function }
    function PenniesToCoins; external 'PENNIESLIB.DLL' name 'PenniesToCoins';
    {$ENDIF}