Ruby win32api & structs (VerQueryValue)

I am trying to call standard Win32 API functions to get file version information using win32-api library .

The functions of 3 version.dll are GetFileVersionInfoSize, GetFileVersionInfo and VerQueryValue. Then I call RtlMoveMemory in kernel32.dll to get a copy of the VS_FIXEDFILEINFO structure (see Microsoft Documentation:) http://msdn.microsoft.com/en-us/library/ms646997%28VS.85%29.aspx.

I learned from the example that I have seen with using the VB: http://support.microsoft.com/kb/139491.

My problem is that the data that is finally being returned does not seem to match the expected structure, in fact it does not even return a consistent value. I suspect that at some point, the data gets crippled, probably in VerQueryValue or RtlMoveMemory.

Here is the code:

GetFileVersionInfoSize = Win32::API.new('GetFileVersionInfoSize','PP','I','version.dll')
GetFileVersionInfo = Win32::API.new('GetFileVersionInfo','PIIP','I', 'version.dll')
VerQueryValue = Win32::API.new('VerQueryValue','PPPP','I', 'version.dll')
RtlMoveMemory = Win32::API.new('RtlMoveMemory', 'PPI', 'V', 'kernel32.dll')

buf = [0].pack('L')
version_size = GetFileVersionInfoSize.call(myfile + "\0", buf)
raise Exception.new  if version_size == 0 #TODO

version_info = 0.chr * version_size
version_ok = GetFileVersionInfo.call(file, 0, version_size, version_info)
raise Exception.new if version_ok == 0   #TODO

addr = [0].pack('L')
size = [0].pack('L')
query_ok = VerQueryValue.call(version_info, "\\\0", addr, size)
raise Exception.new if query_ok == 0        #TODO

# note that at this point, size == 4 -- is that right?

fixed_info = Array.new(13,0).pack('L*')
RtlMoveMemory.call(fixed_info, addr, fixed_info.length)

# fixed_info.unpack('L*')  #=> seemingly random data, usually only the first two dwords' worth and the rest 0.
+5
2

, , , ​​.

/ ( , " " dll):

def file_version ref, options = {}
  options = {:path => LIBDIR, :extension => 'dll'}.merge(options)
  begin
      file = File.join(ROOT, options[:path],"#{ref}.#{options[:extension]}").gsub(/\//,"\\")
      buf = [0].pack('L')
      version_size = GetFileVersionInfoSize.call(file + "\0", buf)
      raise Exception.new    if version_size == 0 #TODO

      version_info = 0.chr * version_size
      version_ok = GetFileVersionInfo.call(file, 0, version_size, version_info)
      raise Exception.new if version_ok == 0        #TODO

      addr = [0].pack('L')
      size = [0].pack('L')
      query_ok = VerQueryValue.call(version_info, "\\\0", addr, size)
      raise Exception.new if query_ok == 0        #TODO

      fixed_info = Array.new(18,0).pack('LSSSSSSSSSSLLLLLLL')
      RtlMoveMemory.call(fixed_info, addr.unpack('L')[0], fixed_info.length)

      fixed_info.unpack('LSSSSSSSSSSLLLLLLL')[5..8].reverse

  rescue
        []
  end
end
+3

fooobar.com/questions/1039745/... : VS_FIXEDFILEINFO FileVersion ProductVersion. , ProductVersion FileVersion. , , , Product- FileVersion , , .

, VS_FIXEDFILEINFO http://msdn.microsoft.com/en-us/library/windows/desktop/ms646997(v=vs.85).aspx , :

typedef struct tagVS_FIXEDFILEINFO {
    DWORD dwSignature;        //  0: L
    DWORD dwStrucVersion;     //  1: S
                              //  2: S
    DWORD dwFileVersionMS;    //  3: S
                              //  4: S
    DWORD dwFileVersionLS;    //  5: S
                              //  6: S
    DWORD dwProductVersionMS; //  7: S
                              //  8: S
    DWORD dwProductVersionLS; //  9: S
                              // 10: S
    DWORD dwFileFlagsMask;    // 11: L
    DWORD dwFileFlags;        // 12: L
    DWORD dwFileOS;           // 13: L
    DWORD dwFileType;         // 14: L
    DWORD dwFileSubtype;      // 15: L
    DWORD dwFileDateMS;       // 16: L
    DWORD dwFileDateLS;       // 17: L
} VS_FIXEDFILEINFO;

5-8, dwFileVersionLS dwProductVersionMS. FileVersion ProductVersion :

info = fixed_info.unpack('LSSSSSSSSSSLLLLLLL')
file_version = [ info[4], info[3], info[6], info[5] ]
product_version = [ info[8], info[7], info[10], info[9] ]
+1

All Articles