In one of my applications, I need to use some DLLs. There may be several versions of those DLLs installed in different directories, and I need to dynamically decide which version of the DLLs to be loaded (assuming that all versions of the DLLs export the same functions, interfaces, and/or classes). What should I do?
For the DLLs that export simple C functions, I can call LoadLibrary() to load them by specifying the full path of the DLLs and then call GetProcAddress() to get the addresses of the functions I want to call. What if I want to use the exported classes of the DLL?
It would be very tedious to get the addresses of the methods of the exported classes by calling GetProcAddress() by ourselves. Fortunately, Visual Studio allows us to specify which DLLs to be delay loaded. The delay loaded DLLs won’t be loaded when the application starts up. Rather, they will be loaded just before the first function of the DLL is called. This is done automatically by the “delay loaded DLLs” feature, which load the DLLs by calling LoadLibrary() and GetProcAddress() for us.
But how do we control from which directory the DLLs should be loaded? The answer is: before any function of the DLLs is called, call SetDllDirectory() by passing the directory from where you want the DLLs to be loaded.