load a library twice

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

load a library twice

Xiangrong Fang
Hi All,

Is the following correct:

var
  h1, h2: TLibHandle;
  lib1, lib2: string;
begin
  lib1 := 'library.so';
  lib2 := 'library.so';
  h1 := LoadLibrary(lib1);
  h2 := LoadLibrary(lib2);
end.

The reason I need this is that I would like to keep the possibility to provide different group of functions in the same or two different libraries. For example, lib1 provides function A, lib2 provide function B, or, lib3 provide both function A and B.

If I open the same library via 2 different handles, are they actually pointing to the same memory address internally, so that if I UnloadLibrary, it will cause something like access violation in the second call?

Thanks!

Xiangrong

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: load a library twice

Anthony Walter-3
Both the Windows LoadLibrary/FreeLibrary and Unix style dlopen/dlclose function internally use a reference counting system to load and unload dll/shared object (dynamic library) files. 

This means that it is safe to call LoadLibrary twice, and FreeLibrary once whilst keeping the a library loading. It's only when the load/unload counts match when a dynamic library will be unloaded.

So to answer you question, yes it's okay.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal