views:

74

answers:

4

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.

+2  A: 

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.

sergiom
+2  A: 

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.

Kirill V. Lyadvinsky
+1  A: 

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.

Sofahamster
The dll has to support delay loading. As far as I know some components like for example xerces have to be modified to work correctly with delay loading.
Totonga
+2  A: 

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("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
}
Totonga