How to determine what DLL's a binary depends on using programmatic methods?
To be clear, I am not trying to determine the DLL dependencies of the running exec, but of any arbitrary exec (that may be missing a required DLL). I'm looking for a solution to implement in a C/C++ application. This is something that needs to be done by my application at runtime and can't be done by a third party app (like depends).
Of course it's possible and easy ! It's even a Win32 FAQ for ages on Win32 api Group
=> a few lines of code with DBAPIs
76 lines to do that based on pedump code (don't forget to add Imagehlp.lib as dependancy):
That's not possible to determine. At least not without a whole lot of work. Any binary can call LoadLibrary to load a DLL. Even if you were to scan the code for all calls to LoadLibrary, you would have to determine what strings were being used to ID the library. Tracking down where in dynamic memory the string has been placed is going to be harder than you want to tackle.
Take a look at the
IMAGE_LOAD_FUNCTION
API. It will return a pointer to aLOADED_IMAGE
structure, which you can use to access the various sections of a PE file.You can find some articles that describe how the structures are laid out here, and here. You can download the source code for the articles here.
I think this should give you everything you need.
Update:
I just downloaded the source code for the article. If you open up
EXEDUMP.CPP
and take a look atDumpImportsSection
it should have the code you need.Dependency Walker can do this by using the profile menu, if you have a target executable. Simply load the executable, tell it to start profiling, and it'll list all the modules loaded while executing the program.
Dependency Walker FAQ (first question...)
In a nutshell, you need to scan the PE file's imports section for each DLL used by the executable. Then recursively locate and scan each dll until you've found all the dependencies.
Of course, apps can use the LoadLibrary family of functions for required or optional functionality. That won't be detected with this method.