Is there a way to simulate LD_LIBRARY_PATH on Windows?

I have a program to make some graphics. When I run it interactively, I want it to use OpenGL from the system to provide hardware accelerated graphics. When I run it in batch mode, I want to be able to redirect it to use the Mesa GL library so that I can use the OSMesa functionality to render to the off-screen buffer. OSMesa can be enabled by running LoadLibrary / GetProcAddress if the batch run option is selected.

On Linux, it's pretty easy to do this job. Using the shell script to invoke the program, I can do something like this:

if [ "$OPTION" = "batch" ]; then export LD_LIBRARY_PATH=$PATHTO/mesalibs:$LD_LIBRARY_PATH fi 

Can I do something on Windows?

When I try to add the directory to the PATH variable, the program continues to go into the opengl32.dll system. The only way I can get the program to use the Mesa GL / OSMesa shared libraries is to be in the same directory as my program. However, when I do this, the program will never use the opengl32.dll system.

+4
source share
4 answers

If I understand that you are speaking correctly, the wrong version of opengl32.dll is loaded when your process starts, i.e. load- dynamic time layout . There is probably no good way to solve your problem without changing this.

You say that it is convenient to use dynamic linking at run time (LoadLibrary / GetProcAddress) for opengl32.dll, because calls to it come from the Qt library. I believe that the Qt library itself is dynamically linked, so you should solve your problem using the runtime binding for this. In this case, when loading the opengl32.dll file before loading the Qt library, you must explicitly indicate which version of opengl32.dll you want to download.

You might want to use load delay to simplify the process of moving from load time to binding at runtime. In this case, the first call to the Qt library forces it to load automatically, and you just need to explicitly load opengl32.dll in the first place.

+4
source

Although this should be possible in the cmd window, it seems you are out of luck.

Try it: set the variable in the script (RUNNING_IN_SCRIPT = Y), and then analyze this variable in your executable file and LoadLibrary from the absolute installation path - be sure to clear the variable when exiting.

0
source

There are several ways to handle this, depending on the libraries and their names / locations:

If both have the same name (opengl32.dll), you need to add the location of the Mesa DLL to the search path so that it is searched to the system directory. Order catalogs are checked in detail here . As you can see, $PATH comes last after the system, so you cannot just add a directory to it. However, you can use the second step ("Current directory") by setting the working directory to the path containing the mesa files. Typically, this means running the application using the absolute path in the directory containing the files.

Nevertheless, it is still not particularly pleasant. If possible, you should use LoadLibrary and check if there is an environment variable ( OPENGL_LIBRARY_PATH ) when the application starts. Assuming the export from opengl32.dll and the Mesa DLL are the same, you can do something like:
 void LoadExports() { char location[MAX_PATH]; getenv("OPENGL_LIBRARY_PATH", location); HMODULE oglLib = LoadLibrary(location); function1 = GetProcAddress(oglLib, "glVertex2f"); ... } 

This will work just fine, doing almost what you want.

However, if you want to do this, you cannot import opengl32.dll , which you probably do, you need to dynamically link everywhere. Do not mess with opengl32.lib and everything should be in order. Depending on how many functions you use, there may be a pain in customization, but the code can be easily written in a script and only need to be done once, you can also use static variables to cache the results throughout the life of the program. It is also possible to use different function names for different libraries, although this requires a bit more logic, so I will leave you the data.

0
source

Windows is used to search for various paths for dynamic libraries, but due to security concerns, the path to the system is first searched.

However, you can use Delay Load Imports to get a workaround:

If you use MSVC, you can highlight the DLLs that you are interested in at boot time using the /DELAYIMPORT flag for the linker.

Then redefine the helper function of the load delay and use the LoadLibrary to find the desired DLL (and not trust it in the system).

After loading the correct DLL, your helper function will simply call the original one, which will do all GetProcAddress .

0
source

Source: https://habr.com/ru/post/1393023/


All Articles