打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Dynamically calling functions in DLLs

Dynamically calling functions in DLLs

By definition DLLs are dynamically loaded libraries of functions and sometimes data. However, it's possible to either hard code the ability to "import" functions from DLLs or dynamically "bind" a DLL during the run time -- which of course means that we don't necessarily need to know the name of the DLL nor the name of the function we're about to call (to a certain extent) during the time we code. Dynamically loading and unloading DLLs could not only save memory, but also can help you write programs that are able to "adjust" itself if certain DLLs are missing.

Following "LoadAndRunDLLProcedure()" function will let you pass the name of the DLL you want to connect to and the name of the function you want to call. If everything goes well, it will load the DLL, call the function, and then unload the DLL.

function LoadAndRunDLLProcedure(  sDLL,  sFunc : string )  : boolean;type  // define the type of "function"  // we're calling  TFunc_Start = procedure;var  Func_Start : TFunc_Start;    hDll       : THandle;  FuncPtr    : TFarProc;  sMsg       : string;begin  Result := False;  hDll   := LoadLibrary(              PChar( sDLL ) );  if(hDll > 32)then  begin    FuncPtr :=      GetProcAddress(        hDll, PChar( sFunc ) );    @Func_Start := FuncPtr;    if(nil <> @Func_Start)then    begin      Func_Start;      Result := True;    end else    begin      sMsg := 'DLL entry point ' +              sFunc + ' not found';      MessageBox(        0, PChar( sMsg ), 'Error',        MB_OK );    end;    FreeLibrary( hDll );  end else  begin    sMsg := 'File ' + sDLL +            ' not found';    MessageBox(      0, PChar( sMsg ), 'Error',      MB_OK );  end;end;

For example, let's say you want to call a procedure called "HelloWorld()" in a DLL named "MyStuff.DLL:"

LoadAndRunDLLProcedure(  'MyStuff.DLL',  'HelloWorld' );

Please note that HelloWorld() must be a procedure, for example, declared as:

procedure HelloWorld;

or in C:

void HelloWorld();

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Dynamically calling an unmanaged dll from .NET (C#)
Delphi中DLL的创建和使用
loadlibrary dll function
Using a Doc/View exported from a dynamically loaded DLL (SDI)
判断当前进程是否有管理员权限
【转载】VB6获取本机IP的API,可以获取局域网IP和互联网IP【恢复】 中国电子开发网...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服