Getting Version of already loaded DLL (Windows API)

╄→гoц情女王★ 提交于 2020-12-08 02:20:52

问题


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

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?


回答1:


Once the DLL is loaded, you can do this:

  1. use GetModuleHandle() to get a handle to the DLL.

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

  3. 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.

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

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.



来源:https://stackoverflow.com/questions/34347999/getting-version-of-already-loaded-dll-windows-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!