views:

47

answers:

3

Using Visual C++ 2008 Express Edition. I'm linking my application with an import library (.lib) for a DLL that might or might not be present on the target system. Before you ask: I cannot distribute the DLL with my application.

If the DLL is not present, as soon as I call a function from the DLL (but not sooner!), I get a message like

This application has failed to start because SomeLibrary.dll was not found. Re-installing the application may fix this problem.

What I want to happen instead, is that the application detects that the DLL is not there, and simply disables functionality that depends on it. I could make a call to LoadLibrary and see whether it succeeded, but I'm not sure whether this is sufficient. Maybe the import library does more work behind the scenes?

Is a simple LoadLibrary call sufficient? If not, what else do I need to do? Can this even be done?

Update: Of course I can use LoadLibrary, and then GetProcAddress for each of the functions I want to use. But that's a hassle, and I was hoping to avoid that and simply use the provided import library instead.

+1  A: 

No, LoadLibrary() is exactly what you want. The only consequence to using it is the hassle of setting up the function pointers into the DLL when you successfully load the DLL, but the process for that is well-covered elsewhere on the net.

Warren Young
I know about that, but I'd rather avoid that hassle and use the provided import library instead. Are you saying I can't have my cake and eat it too?
Thomas
It would be possible to write a tool that loads up an import library and generates the C code for all this, but I'm not aware of such a thing. Thanks for volunteering to write it. :)
Warren Young
+1  A: 

If you go check here (MSDN) you will see that when LoadLibrary fails loading, the function returns a NULL value, and you can even check the specific error (that should be a file not found) using GetLastError function.

jonaspp
This is a perfect match
Ahmed Said
+1  A: 

This is what the DelayLoad linker option is for, but I don't know whether the Express edition supports it.

Henrik
"Delay loading" is indeed the magic word! Thank you so much!
Thomas