Wait until runtime to choose the path to the openGL32.dll that is called

In VS2017 C++ windows application, I want to wait until runtime to choose the path to the openGL32.dll that is called.To facilitate this, I am purposely NOT positioning the openGL32.dll in my application’s folder in Program Files. Instead, I have positioned the openGL32.dll in a folder in the Public directory. So, at runtime, I’m wanting to decide whether to load the openGL32.dll that is in the Public directory, OR load the openGL32.dll that is in the system directory.

However, I’m having a lot of trouble getting this to work. Even though I can get the openGL32.dll in the public directory to successfully load, i cannot seem to stop the openGL32.dll that is in the system directory from ALSO loading.

The evidence that both openGL32.dlls are loading for my application is from dependency walker, the Output window in Visual Studio when running my application in the debugger, and also Process Explorer.

I should point out that in my application’s project settings, I am linking against opengl32.lib, glu32.lib, and glaux.lib.

Here are more details of how I am attempting to do this:

a) In the project settings for my application, I’ve listed the openGL32.dll under the field “Delay Loaded Dlls”.

b) I’ve deleted the openGL32.dll from my application’s directory in Program Files (C:\Program Files (x86)\MyApplication)

c) I’ve put a copy of openGL32.dll into my application’s folder in the Public directory (C:\Users\Public\Documents\MyApplication)

d) In my application’s source code, in the application constructor, I’d added the following two calls:
bResult = SetDllDirectory(“C:\Users\Public\Documents\MyApplicationr\OPENGL32.DLL”);
hLibrary = LoadLibrary(“C:\Users\Public\Documents\MyApplication\OPENGL32.DLL”);

bResult is TRUE;
hLibrary is a valid (not NULL) pointer.

So, with all of the above in place, I successfully get the OPENGL32.DLL in the public directory to be loaded at runtime.This is good. But again, the openGL32.dll in the system folder also gets loaded, which is bad.

Any help would be greatly appreciated!!!

glu32.dll has opengl32.dll as a dependency, so linking against glu32.dll is going to pull in opengl32.dll at load time (and any OpenGL calls which GLU makes will use that DLL, not the one loaded with LoadLibrary). The situation for glaux is probably similar.

This page has more details on how DLLs are located. But I suspect that you’ll probably need to load glu32 and glaux dynamically if you want to select a specific version of opengl32.dll for all uses at run time.

Very interesting. I was thinking that the .lib files were perhaps “interfering” with what I’m trying to do. I’ll try what you suggested (delay loading the glu32.dll and glaux.dll). Do you see any risk / downside to in general delay loading these openGL dlls?

Thanks very much for the help!!!

Only the inconvenience of run-time loading in general.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.