From the title it might seem that the easiest way to do this is by using Loadlibrary. However on many occasions you might end up in a scenario where one or more third party dlls might be in question. For ex, a third party dll is loading A.dll and you also want to load A.dll of a different version. In general , I will be summarizing you how to load 2 different versions of the same dll without using Loadlibrary.
The scenario here is , I have hello.exe which loads intermediate.dll (third party) , intermediatemine.dll(mine) and in turn intermediate.dll will load A.dll (v1.0) and intermediatemine will load A.dll(v2.0). I have the code for intermediatemine.dll , I do not have control for intermediate.dll .Now if we code like below in hello.exe and try to isolate the dlls to be 2 folders , it would not be enough .In the Visual Studio sln , I have hello.exe and intermediatemine.dll and A.dll(v2.0)The folder structure is..\hello\hello\Debug\1 -->contains A.dll (v1.0) & intermediate.dll..\hello\hello\Debug\2 -->contains A.dll(v2.0) & intermediatemine.dll..\hello\hello\Debug--> contains the exeThe below is the an attempt to load the dlls separately which is not sufficient.cout << "Entering Main" << endl;
em = SetErrorMode(SEM_FAILCRITICALERRORS);
HINSTANCE hDll_1 = ::LoadLibrary(L"1\\intermediate.dll");
cout << "Error loading intermediate.dll. Code: "<< ::GetLastError() << endl;
funcptr p_func1 = reinterpret_cast<funcptr>(::GetProcAddress(hDll_1,"SomethingA"));
p_func1(); // This call is in A.dll(v1.0)
funcptr p_func2 = reinterpret_cast<funcptr>(::GetProcAddress(hDll_1,"SomethingB"));
p_func2(); // This call is in A.dll(v1.0)
HINSTANCE hDll_2 = ::LoadLibrary(L"2\\intermediatemine.dll");
cout << "Error loading intermediatemine.dll. Code: " << ::GetLastError() << endl;
funcptr p_func3 = reinterpret_cast<funcptr>(::GetProcAddress(hDll_2, "SomethingC"));
p_func3(); // This call is in A.dll(v2.0)
funcptr p_func4 = reinterpret_cast<funcptr>(::GetProcAddress(hDll_2, "SomethingD"));
p_func4(); // This call is in A.dll(v2.0)
This method uses manifest file to load the dlls.
Step 1 :Firstly create a manifest file like below and save it in the folder where you have the intermediatemine project’s .cpp files,etc .<?xml version="1.0" encoding="UTF-8"standalone="yes"?>
<file name="A.dll" />
</assembly>Save it in ..\hello\hello\intermediatemineStep2 : Now open your Visual Studio and go to Project Properties and change the particular project Property as below .Project Properties-->Manifest Tool-->Input and Output-->Additional Manifest Dependenciesand then add the manifest file as one of the additional dependency ..\hello\hello\intermediatemine\intermediatemine.private.manifestWe do this so, it normally creates A.dll.intermediate.manifest. it then merges that private manifest to make A.dll.embed.manifest ,it then runs the mt.exe as part of the build process to put the embed manifest into the DLL as resource #2.
Note that I have made the A’s Output folder as ..\hello\hello\Debug\2 .
So now when you rebuild your application and run. You will observe that the intermediate.dll from folder 1 loads the A.dll from folder 1 and intermediatemine.dll loads the A.dll from folder 2.I have added the output folder for intermediatemine.dll and A.dll in Visual Studio to Folder 2.Now when I rebuild the application and run I see that the intermediate.dll from folder 1 is loading A from folder 1.