I managed to find the solution. In fact, it only requires some thought - the answer is obvious in retrospect: such a situation can occur when the dll was already loaded before my first LoadLibrary
call. So the 1st call, with the explicit path, loads version 1 - while the 2nd call, without a path, defaults to the first loaded dll (according to the documentation), which happens to be version 2.
This can be fixed by forcefully unloading the library before the first LoadLibrary
call:
HMODULE alreadyLoaded = GetModuleHandle("a.dll");
if (alreadyLoaded) {
FreeLibrary(alreadyLoaded);
}
Although that was enough for me, a more complete solution is to do so in a loop, to handle cases where the dll was loaded multiple times before:
HMODULE alreadyLoaded = GetModuleHandle("a.dll");
while (alreadyLoaded) {
FreeLibrary(alreadyLoaded);
alreadyLoaded = GetModuleHandle("a.dll");
}
Of course, this is potentially dangerous - especially if the original HMODULE
was stored somewhere, as this renders it invalid.