I'm compiling my own DLL comprised of several .o
files. One of the .o
files has a function that calls SHLoadLibraryFromItem
that is supported on Windows 7 only. The function is never called unless the application that uses the DLL is running on Windows 7. (Yes, I'm sure.)
However, when running the application on an older version of Windows (say, XP), the entire application crashes upon launch with an error "The specified procedure could not be found." Although the error doesn't specify which procedure could not be found, if I comment out the call to SHLoadLibraryFromItem
, then everything works fine.
Questions:
SHLoadLibraryFromItem
even though it's not being called on XP?SHLoadLibraryFromItem
only when running Windows 7, i.e., some kind of lazy binding?The only way's around this that I can think of are either:
LoadLibrary
to load the Windows DLL that SHLoadLibraryFromItem
is in and use GetProcAddress
to obtain the address manually into a pointer-to-function and use the pointer to call SHLoadLibraryFromItem
instead?Any other ideas? I'd really prefer some kind of lazy binding as mentioned above.
PLEASE READ WHAT I ACTUALLY WROTE. I clearly stated in the first paragraph that SHLoadLibraryFromItem
is not called unless I KNOW FOR CERTAIN that the application ACTUALLY IS RUNNING ON WINDOWS 7.
The application crashes merely when the DLL is loaded.
The linker embeds a reference in your modules to every API function that's used. when the Windows loader loads your executable and its modules, it needs to "hook up" all the calls in your code to the locations in memory where the API functions are actually loaded. If it can't find them, it won't continue.
Using LoadLibrary
and GetProcAddress
is the "standard" way to get around this problem.
Using two DLLs doesn't help you, because as long as one of them fails to load your app will still not start. You could get around this by using delay-loading, putting all the code that depends on a new O/S in a separate module and wrapping all the calls to that module in Win32 SEH exception handlers (you get an SEH exception when delay-loading cannot load a module). The advantage is that you can use the "automatic" linking without the mess of function pointers, but the exception handling can be pretty nasty.
This article explains a bit and gives some examples of how to wrap this up neatly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With