function MyFun:Integer;stdcall;external 'MyDll.dll';

解决方案 »

  1.   

    静态调用:
    var function test(a:integer):integer;stdcall;external'动态连接库';
      

  2.   

    可参照vcl源码中是如何使用api的,就象上面说的一样
    另外可用loadlibary函数来动态的调用dll中的函数
      

  3.   

    示例:
    DLL源代码:
    library Project2;uses
      SysUtils,
      Classes,
      Dialogs,
      Forms,
      Unit2 in 'Unit2.pas' {Form2};{$R *.RES}
    var
      ccc: Pchar;procedure OpenForm(mainForm:TForm);stdcall;
    var
      Form1: TForm1;
      ptr:PLongInt;
    begin
      ptr:=@(Application.MainForm);
      ptr^:=LongInt(mainForm);
      Form1:=TForm1.Create(mainForm);
    end;procedure InputCCC(Text: Pchar);stdcall;
    begin
      ccc := Text;
    end;procedure ShowCCC;stdcall;
    begin
      ShowMessage(String(ccc));
    end;exports
      OpenForm;
      InputCCC,
      ShowCCC;
    begin
    end.调用方源代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure OpenForm(mainForm:TForm);stdcall;External'project2.dll';
    procedure ShowCCC;stdcall;External'project2.dll';
    procedure InputCCC(Text: Pchar);stdcall;External'project2.dll';procedure TForm1.Button1Click(Sender: TObject);
    var
      Text: Pchar;
    begin
      Text := Pchar(Edit1.Text);
    //  OpenForm(Application.MainForm);//为了调MDICHILD
      InputCCC(Text);//为了实验DLL中的全局变量是否在各个应用程序间共享
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      ShowCCC;//这里表明WINDOWS 32位应用程序DLL中的全局变量也是在应用程序地址空间中,16位应用程序或许不同,没有做实验。
    end;
      

  4.   

    动态调用://==============================================================================
    //获得操作系统版本号************************************************************
    //==============================================================================
    function GetWindowsVersion(var Major, Minor: integer): string;
    var
      {$IFDEF WIN32}
      lpOS, lpOS2: POsVersionInfo;
      OSVerProc: function(lpOs: pointer): BOOL; stdcall;
      OSVerHandle: THandle;
      {$ELSE}
      lp: longint;
      {$ENDIF}
    begin
      {$IFDEF WIN32}
      GetMem(lpOS, SizeOf(TOsVersionInfo));
      lpOs^.dwOSVersionInfoSize := SizeOf(TOsVersionInfo);
      //============================================================================
      //调用DLL例程*****************************************************************
      //============================================================================
      OSVerHandle := LoadLibrary('kernel32');
      if OSVerHandle<=0
      then raise Exception.Create('动态链接库kernel32加载失败,错误代码:'+IntToStr(GetLastError))
      else @OSVerProc := GetProcAddress(OSVerHandle, 'GetVersionExA');
      if not Assigned(OSVerProc)
      then raise Exception.Create('动态链接库kernel32函数GetVersionExA加载失败,错误代码:'+IntToStr(GetLastError))
      else while not OSVerProc(lpOS) do
           begin
             GetMem(lpos2, lpos^.dwOSVersionInfoSize + 1);
             lpOs2^.dwOSVersionInfoSize := lpOs^.dwOSVersionInfoSize + 1;
             FreeMem(lpOs, lpOs^.dwOSVersionInfoSize);
             lpOS := lpOs2;
           end;
      Major := lpOs^.dwMajorVersion;
      Minor := lpOs^.dwMinorVersion;
      FreeLibrary(OSVerHandle);
      FreeMem(lpOs, lpOs^.dwOSVersionInfoSize);
      {$ELSE}
      lp := GetVersion;
      Major := LoByte(LoWord(lp));
      Minor := HiByte(LoWord(lp));
      {$ENDIF}
      case Win32Platform of
        VER_PLATFORM_WIN32_WINDOWS: Result := 'Win9x';
        VER_PLATFORM_WIN32_NT:      Result := 'WinNT';
      end;
    end;
      

  5.   

    随便找本书,上面都有关于dll调用的例子,很详细.
      

  6.   

    Type
    TShowReport=
        REPEDIT(A:THandle;B:TComponent;C:String;D:TStrings;E:Word;F:TFileName):Integer;Stdcall; 
        EDLLLoadError=Class(Exception);
    //调用DLL的函数:
    Function (B:TComponent;C:String;D:TStrings;E:Word;F:TFileName):Integer;Stdcall; implementation{$R *.DFM}Function CallReport(B:TComponent;C:String;D:TStrings;E:Word;F:TFileName):Integer;Stdcall; 
    var
        LibHandle:THandle;//声明 DLL句柄
        ShowCalendar:REPEDIT;//声明调用函数
        Re:Integer;//本地变量接收返回值
    begin
        Re:=0;//初始化返回值
        LibHandle:=LoadLibrary('DLLFILE.Dll');        //加载DLL文件
        Try
            if LibHandle=0 then raise EDLLLoadError.Create('不能加载DLL文件');    //如果失败触发错误并退出
            @ShowCalendar:=GetProcAddress(LibHandle,'TLReportEdit');        //得到DLL文件入口
            if not (@ShowCalendar=nil) then                        //如果有入口则:
                Re:=ShowCalendar(Application.Handle,B,C,D,E,F)
            else
                RaiseLastWin32Error//如果没有DLL入口地址则错误
        Finally
            FreeLibrary(LibHandle);//释放DLL句柄
        end;
        result:=Re;
    end;