Determining existence of DLL before using it

309 views Asked by At

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.

3

There are 3 answers

0
Henrik On BEST ANSWER

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

2
Warren Young On

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.

0
jonaspp On

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.