Load shared library by path at runtime

萝らか妹 提交于 2019-11-27 19:45:30

Un UNIX/Linux systems you can use dlopen. The issue then is you have to fetch all symbols you need via dlsym

Simple example:

typedef int (*some_func)(char *param);

void *myso = dlopen("/path/to/my.so", RTLD_NOW);
some_func *func = dlsym(myso, "function_name_to_fetch");
func("foo");
dlclose(myso);

Will load the .so and execute function_name_to_fetch() from in there. See the man page dlopen(1) for more.

On Windows, you can use LoadLibrary, and on Linux, dlopen. The APIs are extremely similar and can load a so/dll directly by providing the full path. That works if it is a run-time dependency (after loading, you "link" by calling GetProcAddress/dlsym.)

I concur with the other posters about the use of dlopen and LoadLibrary. The libltdl gives you a platform-independent interface to these functions.

I do not think you can do it for it.

Most Dlls have some sort of init() function that must be called after it have been loaded, and sometime that init() function needs some parameters and returns some handle to be used to call the dll's functions. Do you know the definition of the additional library?

Then, the first library can not simply look if the DLL X is in RAM only by using its name. The one it needs can be in a different directory or a different build/version. The OS will recognize the library if the full path is the same than another one already loaded and it will share it instead of loading it a second time.

The other library can load its plugins from another path because it written to not depend on PATH and they are his own plugins.

Have you try to update the process's environment variables from the code before loading the Dll? That will not depends on a starter process.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!