Dynamically calling external libraries.

John Colvin john.loughran.colvin at gmail.com
Wed Feb 26 12:38:26 PST 2014


On Wednesday, 26 February 2014 at 17:58:46 UTC, Mike James wrote:
> On Wednesday, 26 February 2014 at 14:41:02 UTC, Adam D. Ruppe 
> wrote:
>> You'd do it the same way you do in C. On Windows, call 
>> LoadLibrary, FreeLibrary, and GetProcAddress or the COM 
>> functions. On Linux, the family of functions is dlopen, dlsym, 
>> and dlclose.
>>
>> Knowing the types to pass the functions is gonna be tricky and 
>> this needs to be right to avoid crashes. On Windows with 
>> scripting language, this is often done through COM automation: 
>> the IDispatch interface. With regular C functions, you really 
>> just have to know the prototypes ahead of time... it won't be 
>> fully dynamic, you load the library at run time but know how 
>> to use it at compile time.
>
> That's the way I do it but I was wondering. Is it better to 
> load all the functions from the DLL at the start of the program 
> or load them when required and keep having to check if they're 
> loaded before each use?
>
> -<Mike>-

Normally one loads it at the start. In the case of an optional 
feature depending on a shared library, you can always set a 
global flag (probably shared/__gshared) to say whether the load 
was successful and use that later on to check.


More information about the Digitalmars-d-learn mailing list