Bri Bri
Bri Bri

Reputation: 2228

How to handle a missing function in a delay-loaded DLL when using MSVC 16?

In macOS, I can use weak linking to allow use of older versions of a library. In that case, any function I've linked to that's not present in the library resolves to null, and I can check whether the function is available before calling it, e.g.

if (functionName) { functionName(); }

It looks like the closest mechanism in Windows is to use delay-loaded DLLs. (I am specifically compiling with MSVC 16). After reading through their docs, though, it's not clear to me what happens if a procedure in a delay-loaded DLL is missing. What would be most convenient for me is it working just like weak linking on macOS, so that missing functions resolves to null.

Is there a way to do that? Otherwise how does one handle missing procedures in a delay-loaded DLL?

In case it matters, this is a Qt 6.2.1 app.

EDIT: I tried using QueryOptionalDelayLoadedAPI() and found that it does not work. Here is the code I'm using, with DLL and function names replaced. MyFunction takes a single C string argument and returns 0.

qDebug() << QueryOptionalDelayLoadedAPI(GetModuleHandle(NULL), "mydll.dll", "MyFunction", 0);
// Prints 0 erroneously
qDebug() << MyFunction("");
// Prints 0 correctly

EDIT: QueryOptionalDelayLoadedAPI() does work, but requires the mangled name if the function is a C++ function.

Upvotes: 0

Views: 157

Answers (0)

Related Questions