title: ruby win32api & structs (VerQueryValue)
body: <p>I am trying to call the standard Win32 API functions to get file version info, using the <a href="http://rubyforge.org/projects/win32utils/" rel="nofollow noreferrer">win32-api library</a>.  </p>
  
  <p>The 3 version.dll functions are GetFileVersionInfoSize, GetFileVersionInfo, and VerQueryValue.  Then I call RtlMoveMemory in kernel32.dll to get a copy of the VS_FIXEDFILEINFO struct (see Microsoft documentation: <code>http://msdn.microsoft.com/en-us/library/ms646997%28VS.85%29.aspx</code>).  </p>
  
  <p>I drew from an example I saw using VB: <code>http://support.microsoft.com/kb/139491</code>.</p>
  
  <p>My problem is that the data that finally gets returned doesn't seem to match the expected struct, in fact it doesn't even return a consistent value.  I suspect the data is getting mangled at some point, probably in VerQueryValue or RtlMoveMemory.</p>
  
  <p>Here is the code:</p>
  
  <pre><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*')  #=&gt; seemingly random data, usually only the first two dwords' worth and the rest 0.
  </code></pre>