1
votes

I'm using libeay32.dll/ssleay32.dll library in my application for https support. Library is successfully loaded on Windows 7 (not by my application, by Qt library), however I'm getting SSL error on Windows 10.

I wrote a small application to ensure whether correct instance of library is loaded (not from PATH for example) and got interesting result.

Here's the code

HMODULE hSsleay = LoadLibrary("ssleay32.dll");
DWORD pathSize = GetModuleFileName(hSsleay, ssleayPath, pathSize);
if (pathSize) {
    std::cout << "Module ssleay32.dll detected path = " << ssleayPath << '\n';
}
else {
    std::cout << "Module ssleay32.dll detection error LastError = " << GetLastError() << '\n';
}

Output on Windows 7 x86 is

Module ssleay32.dll detected path = C:/Program Files/My Application/ssleay32.dll

However, on Windows 10 x64 it is

Module ssleay32.dll detected path = C:/Program Files (x86)/My Application/LibraryLocator.exe

Where LibraryLocator.exe is a name of a testing application, containing the code above

2
Error checking is crucial, don't let a NULL return value from LoadLibrary() go unnoticed.Hans Passant
If LoadLibrary fails, call GetLastError to find out why. This is explained in the documentation. I urge you to get in to the habit of reading it.David Heffernan
You must call GetLastError() immediately after you detect failure. Don't call any function in between, not even a library function, it can screw up the error value. LoadLibrary error codes are documented everywhere.Hans Passant
GetLastError returns 126 (The specified module could not be found), even though the library is the same directory as an applicationuser707779
That error means that the module you load, or one of its dependencies can't be found. If the module is in the same directory as the executable, it stands to reason that its a dependency that cannot be found.David Heffernan

2 Answers

3
votes

GetModuleFileName() returns the path of the calling process when its hModule parameter is set to NULL. LoadLibrary() returns NULL on failure. So clearly, your call to LoadLibrary() is failing (for instance, if your 32bit EXE tries to load the 64bit version of the DLL, or it can't find the DLL on the PATH since you are not specifying an absolute path). You are not handling the error before calling GetModuleFileName().

If Qt has already loaded the DLL for you, you should be using GetModuleHandle() instead of LoadLibrary().

2
votes

It seems that LoadLibrary call fails and returns a NULL since you never check returned value and pass it directly to GetModuleFileName you obtain the path to the calling executable. Anothing thing is that you need to use wide versions of those functions, e.g. LoadLibraryW, otherwise it may fail on some systems.