大悟还俗

邮箱 key_ok@qq.com 我的收集 http://pan.baidu.com/share/home?uk=1177427271
  新随笔  :: 联系 :: 订阅 订阅  :: 管理

DLL的晚绑定与早绑定

Posted on 2013-10-09 12:13  大悟还俗_2  阅读(369)  评论(0编辑  收藏  举报

调用DLL中的函数可分为早绑定与晚绑定!

早绑定是指在编译期就已经确定函数地址!

晚绑定是指在运行期动态加载dll,并根据查表的方式获取dll内exports函数的地址,由于早绑定比较简单,在此不再讲述,主要说晚绑定! 

//晚绑定,也就是动态调用外部函数主要用以下三个命令:
  //LoadLibrary:获取 DLL
  //GetProcAddress:获取函数
  //FreeLibrary:释放DLL

 

例:

type
  TFunctionName=function():boolean;stdcall;//定义函数类型

  Function DynamicInvokeDllFunction: string;
  var
    DllHnd: THandle;
    FunctionName: TFunctionName;
  begin
    DllHnd := LoadLibrary(PChar('dll文件名'));
    try
      if (DllHnd <> 0) then
      begin
        @FunctionName:=GetProcAddress(DllHnd, 'Dll接口名称');
        if (@FunctionName<>nil) then
        begin
          try
            FunctionName();
          finally

          end;
       end
       else
       begin
          application.MessageBox(PChar('DLL加载出错,DLL可能不存在!'), PChar('错误'),
            MB_ICONWARNING or MB_OK);
       end;
     end;
   finally
      FreeLibrary(DllHnd);
   end;
end;
View Code