以编程方式从函数名称获取序数
在 C++ 中获取导出的 dll 函数的序号(给定其名称)的最简单方法是什么? (寻找一种不涉及自己解析 IAT 的方法......)
What's the easiest way in C++ to get an ordinal of an exported dll function, given its name?
(Looking for a way which doesn't invlove parsing the IATs myself...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想不出任何非常简单的方法来完成你想要的事情。我可以看到你至少有几个选择:
我看到第一个选项的主要问题是您不知道要尝试多少个序数(序数中可能存在间隙,因此依靠
GetProcAddress
返回 NULL 来表示结束将不会'工作)。它的效率也有些低,因为它需要重复进行大量 Win32 调用,并且基本上相当于对导出地址表进行线性搜索。确实很不优雅。作为替代方案,您可以搜索 NPT 并将结果索引用于 EOT 以获得序数。这是一种更优雅的方法,因为它以最直接的方式到达序号(实际上与动态链接器用于将导出名称解析为其地址的方法相同)。另外,因为 NPT 是按词法排序的,所以可以进行二分搜索,这显然比其他方法的线性搜索更可取。事实上,使用此方法实现的对
GetProcOrdinal
的单次调用应该比对GetProcAddress
的一次调用稍快。也许更重要的是,该方法不依赖于任何未知数(即序数的数量)。这种方法的缺点是它不像其他方法那么简单。您可以使用调试帮助库来帮助避免对 PE 文件映像进行一些解析(这就是我最初所做的),但事实证明,解析 PE 映像所需的部分并不那么困难。我认为避免对调试帮助库的依赖值得花费最少的额外精力来解析 PE 映像头。
言归正传,下面是一个用 C 语言实现的示例:
GetProcOrdinal
是有趣的地方。该代码希望是相当不言自明的;然而,要完全理解它可能需要一些关于PE文件格式的知识,我不打算在这里讨论这些(网上有很多关于它的信息)。 FindNptProc 只是一个对 NPT 进行二分搜索的便捷函数。GetExportDirectoryTable
是另一个方便的函数,它解析 PE 标头以定位导出目录表。上面的代码在 Visual Studio 2008 和 Windows XP (SP3) 下对我来说可以干净地编译,但是 YMMV。我并不是真正的 Windows 用户*,因此这可能不是最干净的代码可移植性(就不同版本的 Windows 而言)。像往常一样,此代码“按原样”提供,不提供任何形式的保证;)
*是的,以防万一您想知道,在编写了所有 Microsoft 风格的内容后,我确实仍然感觉有点肮脏Windows 代码。
I can't think of any terribly simple way to do what you want. You have at least a couple of options that I can see:
The main problem I see with the first option is that you don't know how many ordinals to try (there can be gaps in the ordinal numbers, so counting on
GetProcAddress
returning NULL to signal the end won't work). It's also somewhat inefficient because it requires making a lot of Win32 calls repeatedly and it basically amounts to a linear search of the export address table. Pretty inelegant, indeed.As an alternative, you can search the NPT and use the resultant index into the EOT to obtain an ordinal. This is a more elegant approach because it arrives at the ordinal in the most direct way possible (it is actually the same method the dynamic linker uses to resolve export names to their addresses). Also, because the NPT is lexically sorted, it's possible to do a binary search which is obviously preferable to the other method's linear search. In fact, a single call to
GetProcOrdinal
implemented with this method should be slightly faster than just one call toGetProcAddress
. Perhaps more importantly, this method doesn't depend on any unknowns (i.e. number of ordinals). The disadvantage to this method is that it's not as simple as the other method.You could use the Debug Help Library to help avoid doing some of the parsing of the PE file image (this is what I did initially), but it turns out that parsing the required parts of the PE image is not that difficult. I think avoiding the dependency on the Debug Help Library is worth the minimal extra effort required to parse the PE image headers.
Getting down to business, here is an example implementation in C:
GetProcOrdinal
is where the interesting bits happen. The code is hopefully fairly self-explanatory; however, to fully understand it may require a bit of knowledge about the PE file format, which I'm not about to get into here (there's plenty of info on the web about it).FindNptProc
is simply a convenience function that does the binary search of the NPT.GetExportDirectoryTable
is another convenience function that parses the PE headers to locate the export directory table.The code above compiles cleanly for me under Visual Studio 2008 and Windows XP (SP3), but YMMV. I'm not really a Windows guy*, so this might not be the cleanest code portability-wise (in terms of different versions of Windows). As usual, this code is provided "as is" with no warranty of any kind ;)
*Yes, in case you're wondering, I do still feel kind of dirty after writing all that Microsoft-style Windows code.
一种丑陋的方法是使用 dumpbin 命令运行系统调用并解析输出。但这与众所周知的瓷器店里的公牛几乎一样优雅。
dumpbin /exports c:\windows\system32\user32.dll | grep FunctionOfInterest
否则,您可以编写一个简单的循环,使用序数调用 GetProcAddress (在名称参数的低两个字节中传递)。当函数指针与传递实际名称时返回的指针匹配时,就完成了。
这是没有错误检查的基本思想:
An ugly way would be to run a system call with a dumpbin command and parse the output. But that has about the same elegance as a bull in the proverbial china shop.
dumpbin /exports c:\windows\system32\user32.dll | grep FunctionOfInterest
Otherwise, you could write a simple loop calling GetProcAddress with ordinals (passed in the low two bytes of the name parameter). When the function pointer matches the pointer returned when passing the actual name, then you are done.
Here is the basic idea without error checking: