问题
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:
use
GetModuleHandle()
to get a handle to the DLL.use that handle with
FindResource()
/LoadResource()
/LockResource()
to access the DLL'sRT_VERSION
resource data.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.pass that memory block to
VerQueryValue()
to access itsVS_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