soulsabr
soulsabr

Reputation: 904

How do I programmatically list a DLL's dependencies in C++ or Python?

I'm currently programming a Python interface for a C++ project using Boost Python. The problem is that if a DLL is missing Python gives us a very unhelpful error message:

ImportError: DLL load failed: The specified module could not be found.

According to this site it isn't possible to display more information than this.

Now, the big issue. I cannot use dependency walker or dumpbin because we need to be able to programmatically determine which DLL is missing. The good news is that we are only required to check the first level of dependencies. So, if my.exe is dependent on a.dll, b.dll, and c.dll then that is the only set of DLLs we're interested in. If a, b, and c are all where they should be then my job is done.

I have already found this MSDN page on enumerating for a running process but have been unable to find out how to do so for a non-running .exe or unloaded .dll. The closest I've come is an MSDN article about the LoadLibraryEx function*. However, I cannot for the life of me figure out how to get the dependency table from the returned HMODULE.

So, the $64,000 question is: how do I get the .exe/.dll dependencies from the HMODULE? An even better question is: Is that where I get the dependencies? If not then where can I find it?

The optimal solution would be in C++ but we're more than happy to have a Python solution as well. Any help or suggestions would be appreciated. Thanks.

* I'd link the article but my rep isn't high enough to post two links in one question. :)

Upvotes: 4

Views: 2067

Answers (1)

Nick Cano
Nick Cano

Reputation: 457

You need to read the Portable Executable Header (PE Header) of the module. This is a structure that describes the imports, exports, re-locations, sections, resources, code, static data, and everything else the binary relies on. While it can be parsed directly, the PE Header structure has a lot of quirks and nuances that aren't obvious. I'd recommend using a library, such as PeLib, to handle everything for you.

Upvotes: 4

Related Questions