Context sharing between EXE and DLL

Hi,

I’m making a program with a hot-reloading system, where most of the code is compiled to a DLL and the EXE reloads it whenever a change is made. I’m initializing the window in the EXE and trying to sharing the context with the DLL so that the DLL can draw to it with opengl functions, but I’m running into issues with GLAD loading function pointers…

I’m using these lines within the DLL to attempt to load function pointers:

// game_memory is passed to the DLL and game_memory->window is a GLFWwindow * set in the EXE after window initialization
glfwMakeContextCurrent(game_memory->window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)

This exact same code works just fine in the EXE, and I’ve been able to draw triangles, use shaders, etc. But when I use it in the DLL it fails, and I don’t understand why. Is context not allowed to be shared between an EXE and DLL? Or is there something else going on?

How is glfw linked for that exe and dll? Are you using glfw shared library? Because then it should just work, even without calling glfwMakeContextCurrent. But if you have linked glfw statically in both exe and dll, then it won’t work, because there are two copies of glfw, and only one is initialized properly (in exe).

Make sure you have glfw linked dynamically as dll file to both your exe and dll. Alternatively, if you really want static linking to glfw, then link only in exe, and export glfw functions from exe, so dll can use them from exe.

1 Like

That fixed it! I was statically linking to both EXE and DLL, and it worked after switching to linking to a DLL. Thanks!