Reputation: 22932
I have a 3rd party component that includes a .LIB and .DLL file. In order to use the component I link the .LIB into my C++ program, and distribute the .DLL with application. The functionality provided is very specific, and only relevent to a small sub-set of my users, but distributing the .DLL incurs a license fee.
One work around here is to have two versions of my app, one which links in the 3rd party component, the other that doesn't, but I'd rather avoid the extra time involved in maintaining and distributing a second build.
Ideally, I'd like to simply exclude the .DLL from the distribution, but if I do this I get the error 'This application has failed to start because XXXXX.DLL was not found. Re-Installing the application may fix this problem'. Is this an exception that I can catch and deal with in my code? Alternatively, can I delay the loading of the .DLL until an attempt is made to call the specific functionality provided, and handle it then, or simply check for the existence of the .DLL and act accordingly?
The environment is VS 2003 and VS 2008.
Upvotes: 3
Views: 3047
Reputation: 4366
There is no way to stop the binding after linked with the dll. The only way that youo have is if you dynamically load the dll during runtime.
Dll resolving is done before your exe starts running. Code could look somehow like that. If this does not work for your third party dll you could write an own dll that wrapps the third party dll and which can be loaded dynamically at runtime.
HINSTANCE lib = LoadLibraryEx("C:\dlls\thirdparty.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if(0 != lib) {
// Dll is present so use it
typedef CObj ( __cdecl *tFunction ) (const wchar_t*, const int&);
tFunction functionEntry = (tFunction)(GetProcAddress( lib,"EntryFunction"));
assert(0 != functionEntry);
// call the function
CObj obj = functionEntry(L"Hello", 1);
}
else {
// dll not present
}
Update: Please make sure that you use fullpath to your dll to make sure not any dll is pulled that has this name.
Upvotes: 6
Reputation: 761
Visual Studio has support for delay-loaded DLLs. Using this functionality, a DLL is loaded when you call a function from that library for the first time. But you should check the license of your component to see if that's allowed.
Upvotes: 5
Reputation: 99625
You could use LoadLibrary
function to explicitly load DLL and the check the result. If successful then use GetProcAddress
to find your CreateMyInterfaceImpl
function. Otherwise use fake implementation of your interface or don't use it at all.
Upvotes: 3
Reputation: 4887
You could load the DLL dynamically using the LoadLibrary() API function But then you must use the GetProcAddress() on every function exported by the DLL that your application needs to call.
Upvotes: 3