DelphiFAQ Home Search:

Dynamic loading and binding of DLLs

 

comments2 comments. Current rating: 4 stars (2 votes). Leave comments and/ or rate it.

Sometimes you may need to load a DLL at runtime, for example

  • if you have a couple of different DLLs to choose between
  • to have a concept for optional functionality.
This small source code shows how to load the DLL with LoadLibrary and use the returned handle to access (bind) the functions that are contained:

{ function declaration }

type
  TfncCnx = function (s : string) : THandle;

var
  cnx : TfncCnx;
begin
  { load the DLL and get the function's address }
  h := LoadLibrary('myDll');
  if h = 0 then
  begin
    ShowMessage ('DLL not available');
  end
  else
  begin
    @cnx := GetProcAddress(h, 'myProc');
    if @cnx = nil then 
    begin
      { function not found.. misspelled? }
      ShowMessage ('blub');
    end
	else
    begin
      { call the function as usually }
      x := cnx('alpha');
    end;
    { unload the DLL }
    FreeLibrary(h);
  end;
end;

Comments:

2006-05-10, 10:01:57
welmers_aj@yahoo.com from United States  
rating
thanks foor the snippet. it is clear and usefull
2007-01-12, 00:57:57
ALC from Indonesia  
rating
OK


Keywords: cartoon

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: