c++windowswinapidlldelay-load

What's an alternative method to QueryOptionalDelayLoadedAPI to tell if a function from a delay-loaded DLL is available that will work in Windows 7?


I'm writing a C++ Windows desktop app that makes use of delay-loaded DLLs in order to support different versions of the DLL that has different functions available.

If I were targeting Windows 10 and later, I could use the function QueryOptionalDelayLoadedAPI to tell if a function is available. Unfortunately, I need to target Windows 7 as well and QueryOptionalDelayLoadedAPI isn't available there.

What's another method I can use to detect of a delay-loaded function is available that works in Windows 7?

edit: To be clear, I need to check if the function is available without trying to call it.


Solution

  • Without actually calling the function, the only option you have in this case is to load the DLL into memory via LoadLibrary/Ex() and then use GetProcAddress() to get a pointer to the desired function. If either fail, the function is not available. If both are successful, you will have a pointer with which you can call the function, if desired.

    This is essentially all the delay-loading mechanism does internally (besides its notification hooks).

    What you could do is wrap this logic into your own function, and then delay-load QueryOptionalDelayLoadedAPI() itself, using a failure hook to let you know when QueryOptionalDelayLoadedAPI() is not available so you can map it to your custom function instead. This way, the rest of your code can just call QueryOptionalDelayLoadedAPI() unconditionally on all Windows versions, and your custom function will be called on older versions.

    Or, you can just do the same thing for other missing DLL functions. Just write your own replacement functions that mimic the official functions, and/or return error codes that your code can look for, like ERROR_NOT_IMPLEMENTED, etc.