views:

1421

answers:

7

Anybody know 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).

Thanks. -Kelly

+1  A: 

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.

Steve Rowe
Why vote me down here? This answer is technically accurate as far as I can tell.
Steve Rowe
My guess on why this answer was voted down: it doesn't distinguish between implicit dependencies (which can be determined, see alex2k8's link) and explicit dependencies (which is what you're talking about). Don't be gloomy, the answer was half right!
jdigital
+1  A: 

Check this question.

alex2k8
+4  A: 

Take a look at the IMAGE_LOAD_FUNCTION API. It will return a pointer to a LOADED_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 at DumpImportsSection it should have the code you need.

Scott Wisniewski
Thanks for you suggestion. Especially for the links to the source examples. Exactly what I was looking for.
Kelly
+1  A: 

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.

zildjohn01
A: 

some help, the link for download DEPENDS

lsalamon
A: 

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

A: 

How about a DLL that you can call to calculate all this information for you and pass back the answer as an array of CStrings?

PE Format DLL can do this for you. Supplied with source code, no GPL restrictions. PE File Explorer is a GUI app that uses the DLL, also supplied with source (no GPL).

Stephen Kellett