Home > other >  Detect missing dlls by C program using it?
Detect missing dlls by C program using it?

Time:01-11

Is it somehow possible to run .exe without some of the (mainly 3rd party) dlls which may (or may not) be present? The reason for this is that if some of the drivers are not present, I want only to block using the HWs, not to block program execution.

For example: Run .exe which checks if there is desired .dll libraries and make action if not:

  • Warn user that some of the libraries are missing.
  • Forbid to use some part of code until the appropriate library is present.

I found out, that library checking is possible with function like this:

bool checkLibrary(std::string dllName) {
    std::wstring stemp = std::wstring(dllName.begin(), dllName.end());
    return LoadLibrary(stemp.c_str()) != NULL;
}

But when I run .exe, it shows me allways error, that .dll is missing. It looks like that OS or APP finds all dependecies before start of the .exe, but seems to be static linked. How link dlls dynamically when its functions is needed?

Thanks in advance for you answers.

CodePudding user response:

If you use headers you automatically link to library so on .exe loading it looks for that library. If you want to manually load DLL, you need to open it as you did and replace all calls by calls to place given by https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress

CodePudding user response:

This is an option in Visual Studio. It's called Delay-Load Linking (/DELAYLOAD). By default, Visual Studio will set it up entirely for you, including the call to LoadLibrary.

Since you want to be notified about load failures, you'll need to set up a hook function. This will need to handle the dliFailLoadLib notification. Here you can show a custom error message, and set a "disabled" flag for your app's internal use.

Continuing with missing functions is a bit trickier. You'll need to handle more events though. If you set a "disabled" flag for internal use, you could just return &ExitProcessWrapper for each and every function that's not supposed to be called. This wrapper just calls ExitProcess(0), in case you overlooked something.

  •  Tags:  
  • Related