获取已经加载的DLL的版本(Windows API) [英] Getting Version of already loaded DLL (Windows API)

查看:413
本文介绍了获取已经加载的DLL的版本(Windows API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在加载之前获取dll的文件版本很容易。即时通讯使用GetFileVersionInfoSize + GetFileVersionInfo + VerQueryValue,它的工作原理像一个超级按钮。

ok, getting the file version of a dll before loading, is easy. im using GetFileVersionInfoSize + GetFileVersionInfo + VerQueryValue and it works like a charm.

但是如果dll已经加载怎么办?我知道您可以使用LoadLibrary + IMAGE_DOS_HEADER + IMAGE_NT_HEADERS来检索某些信息,例如函数名称等。我注意到IMAGE_OPTIONAL_HEADER具有不同的版本字段,例如MajorImageVersion和MinorImageVersion等。我几乎尝试了所有操作,但是这些字段并不总是设置,并且在设置时,它们与GetFileVersionInfo返回的值不匹配。所以我猜我看错地方了。有任何想法吗?

but what if the dll is already loaded? i know you can use LoadLibrary + IMAGE_DOS_HEADER + IMAGE_NT_HEADERS to retrieve certain information such as the function names etc. i noticed that IMAGE_OPTIONAL_HEADER has different version fields such as MajorImageVersion & MinorImageVersion etc. i tried pretty much everything, but those fields are not always set and when they are, they dont match the values returned by GetFileVersionInfo. so im guessing im looking in the wrong place. any ideas?

推荐答案

一旦加载了DLL,您可以执行以下操作:

Once the DLL is loaded, you can do this:


  1. 使用 GetModuleHandle()获取DLL的句柄。

使用带有 FindResource() / LoadResource() / LockResource()访问DLL的 RT_VERSION 资源数据。

use that handle with FindResource()/LoadResource()/LockResource() to access the DLL's RT_VERSION resource data.

将该资源数据复制到您分配的内存块中(重要!)。使用 SizeofResource()知道要分配和复制多少字节。

make a copy of that resource data to a memory block you allocate (important!). Use SizeofResource() to know how many bytes to allocate and copy.

将该内存块传递给 VerQueryValue()访问其 VS_FIXEDFILEINFO 结构,其中包含DLL的版本号。

pass that memory block to VerQueryValue() to access its VS_FIXEDFILEINFO structure, which contains the DLL's version numbers.

第3步很重要,因为 VerQueryValue()需要访问可写内存(它依赖于各种在内存内容中进行的修正)。 LockResource()返回的内存指针指向只读内存。如果尝试直接使用资源指针,则 VerQueryValue()将崩溃。

Step 3 is important because VerQueryValue() needs access to writable memory (it relies on various fixups made within the content of the memory). The memory pointer returned by LockResource() is pointing at read-only memory. If you attempt to use the resource pointer directly, VerQueryValue() will crash.

这篇关于获取已经加载的DLL的版本(Windows API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆