Home > Software design >  How can I prevent linking to a specific function in a DLL, in order to maintain compatibility with a
How can I prevent linking to a specific function in a DLL, in order to maintain compatibility with a

Time:10-23

The short version of the question:

Is it possible for me to instruct Microsoft's command line C compiler to link against a dynamic library and tell the linker not to link against one specific function in that DLL, given the function's name (mangled or unmangled) or perhaps its ordinal, in order to preserve compatibility with earlier versions of the library's DLL that don't include that function?

The long explanation:

I'm developing a DLL that's a plug-in for Autodesk Maya 2020. This DLL needs to link against one of Maya 2020's dynamic libraries named OpenMayaUI.

Normally plug-ins built for one major version of Maya (such as 2020) are supposed to be binary compatible with any other minor versions of that major version. For example, a plug-in built using Maya 2020.4 should still load in Maya 2020.0.

However, Autodesk messed this up and introduced a single new function in Maya 2020.3's version of OpenMayaUI.dll that's not present in Maya 2020.0, Maya 2020.1, or Maya 2020.2. Consequently, if I build my plug-in so that it's linking to the version of OpenMayaUI.lib that comes from Maya 2020.3 or 2020.4, trying to load the plug-in in an earlier version of Maya 2020 will fail with the error "the specified procedure cannot be found". (And this is correct, the procedure isn't provided in the earlier versions of OpenMayaUI.dll.) However, linking against Maya 2020.0's version of OpenMayaUI.lib will produce a DLL that can be loaded by any version of Maya 2020.

Unfortunately, the script that builds my plug-in needs to be able to be run on a system with any version of Maya 2020 installed, including 2020.3 and 2020.4. But I need the builds it generates to be compatible with Maya 2020.0 nonetheless.

So, is it possible for me to link against the Maya 2020.4 version of OpenMayaUI.lib, but somehow tell the linker to avoid linking to the specific function in that dynamic library that's not present in the Maya 2020.0 version of OpenMayaUI? In this case I know the exact name of the function, both mangled and unmangled, and I know its ordinal in both the 2020.0 and 2020.4 versions of OpenMayaUI.dll.

I'm building the plug-in using Microsoft's command line C compiler included with Visual Studio 2019.

Edit with additional information: The offending function is a virtual member function in one of Maya's C classes, so even though I'm not calling it, my DLL still gets linked against it.

CodePudding user response:

is it possible for me to link against the Maya 2020.4 version of OpenMayaUI.lib, but somehow tell the linker to avoid linking to the specific function in that dynamic library that's not present in the Maya 2020.0 version of OpenMayaUI?

Yes. Tell the linker that OpenMayaUI.dll is to be delay loaded.

Linker support for delay-loaded DLLs

That way, every function that your code calls from that DLL will not be linked to statically in your plugin's IMPORTS table, but rather will be loaded at runtime via LoadLibrary() and GetProcAddress() the first time they are called. This way, your plugin has the opportunity to verify the DLL's version before calling the missing function.

  • Related