<p>Is there a way in Ruby to find the version of a file, specifically a .dll file?</p>

[{'answer_id': 76554, 'author': 'cynicalman', 'author_id': 410, 'author_profile': 'https://Stackoverflow.com/users/410', 'pm_score': 1, 'selected': False, 'text': "<p>For any file, you'd need to discover what format the file is in, and then open the file and read the necessary bytes to find out what version the file is.  There is no API or common method to determine a file version in Ruby.</p>\n\n<p>Note that it would be easier if the file version were in the file name.</p>\n"}, {'answer_id': 76768, 'author': 'Erik', 'author_id': 13623, 'author_profile': 'https://Stackoverflow.com/users/13623', 'pm_score': 2, 'selected': False, 'text': '<p>If you are working on the Microsoft platform, you should be able to use the Win32 API in Ruby to call GetFileVersionInfo(), which will return the information you\'re looking for.\n<a href="http://msdn.microsoft.com/en-us/library/ms647003.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms647003.aspx</a></p>\n'}, {'answer_id': 77447, 'author': 'AShelly', 'author_id': 10396, 'author_profile': 'https://Stackoverflow.com/users/10396', 'pm_score': 4, 'selected': False, 'text': '<p>For Windows EXE\'s and DLL\'s:</p>\n\n<pre><code>require "Win32API"\nFILENAME = "c:/ruby/bin/ruby.exe" #your filename here\ns=""\nvsize=Win32API.new(\'version.dll\', \'GetFileVersionInfoSize\', \n                   [\'P\', \'P\'], \'L\').call(FILENAME, s)\np vsize\nif (vsize &gt; 0)\n  result = \' \'*vsize\n  Win32API.new(\'version.dll\', \'GetFileVersionInfo\', \n               [\'P\', \'L\', \'L\', \'P\'], \'L\').call(FILENAME, 0, vsize, result)\n  rstring = result.unpack(\'v*\').map{|s| s.chr if s&lt;256}*\'\'\n  r = /FileVersion..(.*?)\\000/.match(rstring)\n  puts "FileVersion = #{r ? r[1] : \'??\' }"\nelse\n  puts "No Version Info"\nend\n</code></pre>\n\n<p>The \'unpack\'+regexp part is a hack, the "proper" way is the VerQueryValue API, but this should work for most files. (probably fails miserably on extended character sets.)</p>\n'}, {'answer_id': 142578, 'author': 'Tom Lahti', 'author_id': 22902, 'author_profile': 'https://Stackoverflow.com/users/22902', 'pm_score': 3, 'selected': False, 'text': '<p>What if you want to get the version info with ruby, but the ruby code isn\'t running on Windows?  </p>\n\n<p>The following does just that (heeding the same extended charset warning):</p>\n\n<pre><code>#!/usr/bin/ruby\n\ns = File.read(ARGV[0])\nx = s.match(/F\\0i\\0l\\0e\\0V\\0e\\0r\\0s\\0i\\0o\\0n\\0*(.*?)\\0\\0\\0/)\n\nif x.class == MatchData\n  ver=x[1].gsub(/\\0/,"")\nelse\n  ver="No version"\nend\n\nputs ver\n</code></pre>\n'}, {'answer_id': 17068756, 'author': 'Pete', 'author_id': 131887, 'author_profile': 'https://Stackoverflow.com/users/131887', 'pm_score': 2, 'selected': False, 'text': '<p>As of Ruby 2.0, the <code>DL</code> module is deprecated. Here is an updated version of AShelly\'s answer, using <a href="http://www.ruby-doc.org/stdlib-2.0/libdoc/fiddle/rdoc/Fiddle.html" rel="nofollow">Fiddle</a>:</p>\n\n<pre><code>version_dll = Fiddle.dlopen(\'version.dll\')\n\ns=\'\'\nvsize = Fiddle::Function.new(version_dll[\'GetFileVersionInfoSize\'],\n            [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],\n            Fiddle::TYPE_LONG).call(filename, s)\n\nraise \'Unable to determine the version number\' unless vsize &gt; 0\n\nresult = \' \'*vsize\nFiddle::Function.new(version_dll[\'GetFileVersionInfo\'],\n                     [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG,\n                      Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP],\n        Fiddle::TYPE_VOIDP).call(filename, 0, vsize, result)\n\nrstring = result.unpack(\'v*\').map{|s| s.chr if s&lt;256}*\'\'\nr = /FileVersion..(.*?)\\000/.match(rstring)\n\nputs r[1]\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76472', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']