Hey everyone
I have a project that was written in C++ and Rust which depends on MSVCP140 from MSVC.
In some computers it's missing, but windows doesn't provide useful error message for the users.
I want to detect it before the binary crash and open error message with link to download it.
Does that possible?
If you want to handle missing DLL's, you have to use LoadLibrary at runtime to access the DLL rather than making your executable directly declare a dependency on the DLL. The error when a DLL marked as dependency is missing happens in the dynamic linker before any of your own code gets a chance to run. With LoadLibrary you can run your own code before loading the DLL and handle the error when loading the DLL fails.
In the case of MSVCP140 however you really need a direct dependency. You may be able to use ucrt instead of MSVCP140 however. Microsoft added ucrt retroactively to all Windows versions starting from Windows Vista using an update.
There is a standard workaround for detecting onload DLL dependencies, though.
Well, in this case it's an installer that ensures the appropriate redistributable libraries are available.
But the general solution to provide nicer errors is to have a shim library. The shim checks that all of the relevant libraries can be loaded (in the dependency order, to avoid implicit loading), reporting any errors along the way, finalized by loading the actual library and calling into the actual work to be done.
For libraries that are required to be loaded at startup and can't be late loaded, this won't work, but it can be used in a reasonable amount of cases. I've seen at least one custom import .lib
which did this to make a custom error when its DLL was missing instead of relying on the OS error.
@thewh1teagle looks like he has an executable and not a library however. Does not this mean that he can create a shim executable which does the following:
- Load all dependency DLLs as datafiles (without executing any code). Should probably work regardless of whether library needs to be loaded at startup, it is not like shim is expected to do any actual work.
- Report any errors to the user and stop here if there were some.
- Call actual executable with all arguments which were supplied to the shim.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.