列位师哥:
  我刚刚接触DELPHI的DLL,看了一些资料但是还是很不明白。
我写了一个很简单的DLL,如下:
library test_dll;
uses
  SysUtils,
  Dialogs,
  Classes;function c_sum(r:integer):integer;stdcall;
begin
  result:=r*3;
end;{$R *.res}
exports
c_sum;
begin
end.并在主程序里调用
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
function c_sum(r:integer):integer;stdcall;external'test_dll.dll';procedure TForm1.Button1Click(Sender: TObject);
var
 s:integer;
begin
 s:=strtoint(edit1.text);
  edit2.Text:=inttostr(c_sum(s));
end;end.
这里用的是静态调用一切OK。(当edit1.text为3时,edit2.Text显示9)可是换成动态调用
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    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
 s:integer;
 t_handle:Thandle;
 test_sum:function(r:integer):integer;
begin
 s:=strtoint(edit1.text); t_handle:=loadlibrary('test_dll.dll');
 if t_handle<32 then
   begin
     application.MessageBox('动态连接库掉失!','提示',64);
     exit;
   end
   else begin
          @test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
          edit2.Text:=inttostr(test_sum(s));
          FreeLibrary(t_handle);
        end;
end;end.
却发生了错误,(当edit1.text为3时,edit2.Text居然显示3731172)
这是为何呢?
还有就是动态调用中
为何@test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
Getprocaddress函数不能根据句柄t_handle,直接取函数名吗?一定要取
函数序号?
请各位师哥多赐教,先谢过了.

解决方案 »

  1.   

    GetProcAddress(DLL_Handle, '函数名称')    <------给出DLL,输出的函数名称The GetProcAddress function returns the address of the specified exported dynamic-link library (DLL) function. FARPROC GetProcAddress(    HMODULE hModule, // handle to DLL module  
        LPCSTR lpProcName  // name of function 
       );
     ParametershModuleIdentifies the DLL module that contains the function. The LoadLibrary or GetModuleHandle
     function returns this handle. lpProcNamePoints to a null-terminated string containing the function name, or specifies the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.
      

  2.   

    首先谢谢楼上的师哥
    GetProcAddress(DLL_Handle, '函数名称')
    让我知道此用法。
    不过问题依旧,
    动态调用仍然出错即(当edit1.text为3时,edit2.Text居然显示3731172)
    为何呢?    
      

  3.   

    试验结果发现,申明test_sum出了问题,必须后边加Stdcall以表明是远程调用。
      

  4.   

    test_sum: function (r:integer):integer;stdcall; // 加上调用方式
      

  5.   

    Getprocaddress 函数可以根据句柄t_handle 、函数名得到函数地址
      

  6.   

    TO (海盗)与 (计算机质子) 
     谢谢两位的答案问题解决了。马上结帐,并请到小M的另一个贴子上再指教谢谢。
    http://expert.csdn.net/Expert/topic/1775/1775482.xml?temp=7.564723E-03