简体   繁体   English

如何以编程方式在C ++或Python中列出DLL的依赖项?

[英]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. 我目前正在使用Boost Python为C ++项目编写Python接口。 The problem is that if a DLL is missing Python gives us a very unhelpful error message: 问题是如果缺少DLL,Python会给我们一个非常无用的错误消息:

ImportError: DLL load failed: The specified module could not be found. ImportError:DLL加载失败:找不到指定的模块。

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. 我不能使用依赖walker或dumpbin,因为我们需要能够以编程方式确定缺少哪个DLL。 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. 因此,如果my.exe依赖于a.dll,b.dll和c.dll,那么这是我们感兴趣的唯一一组DLL。如果a,b和c都是它们应该存在的地方那么我的工作完成了。

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. 我已经发现这个MSDN页面枚举正在运行的进程,但是无法找到如何为非运行的.exe或卸载的.dll执行此操作。 The closest I've come is an MSDN article about the LoadLibraryEx function*. 我最接近的是关于LoadLibraryEx函数*的MSDN文章。 However, I cannot for the life of me figure out how to get the dependency table from the returned HMODULE. 但是,我不能为我的生活弄清楚如何从返回的HMODULE获取依赖表。

So, the $64,000 question is: how do I get the .exe/.dll dependencies from the HMODULE? 因此,64,000美元的问题是:如何从HMODULE获取.exe / .dll依赖项? 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. 最佳解决方案是使用C ++,但我们也非常乐意拥有Python解决方案。 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. *我会链接文章,但我的代表不够高,不能在一个问题中发布两个链接。 :) :)

You need to read the Portable Executable Header (PE Header) of the module. 您需要读取模块的可移植可执行标头(PE标头)。 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. 虽然它可以直接解析,但PE Header结构有很多怪癖和细微差别并不明显。 I'd recommend using a library, such as PeLib, to handle everything for you. 我建议使用像PeLib这样的库来处理所有事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM