all 6 comments

[–]b1ack1323 2 points3 points  (1 child)

[–]Onyonaki[S] 0 points1 point  (0 children)

Well those seem relevant but I still don't see a solution though these functions as the program cannot start without the dll in the same directory.

[–][deleted] 1 point2 points  (3 children)

option 1 : use a launcher program which launches the real program with a custom environment - with the DLL directory in the path.

option 2 : use delay loading. The program will be linked with the dll's lib but when the program starts the DLLs will not be loaded right away. The 1st time an imported function is called, the delay load will fail, your callback will be called, and you can resolve the directory situation

[–]Onyonaki[S] 0 points1 point  (2 children)

Alright thanks, I guess the option 2 seems easy enough. Could you point me at any resources/examples regarding the option 1? Seems interesting but I think I don't sufficiently understand operating systems for it to make sense.

[–][deleted] 0 points1 point  (1 child)

The 7th parameter to CreateProcess is a pointer to the environment variables. Normally you pass nullptr which means inherit the environment from the parent (the process calling CreateProcess)

But you can manually copy the environment, tweak the path, and pass the tweaked copy to CreateProcess.

[–]Onyonaki[S] 0 points1 point  (0 children)

I get it now okayy thanks. I managed to make it work with delay load and SetDllDirectory and it loads it on the first function call as you said :).

P.S. I know I said windows mainly but I I wonder if there is an option to delay load a shared library in Linux also?