【问题标题】:Finding the version of an application from Python?从 Python 中查找应用程序的版本?
【发布时间】:2011-01-17 05:46:07
【问题描述】:

基本上,我试图找出用户当前安装的 ArcGIS 版本,我查看了注册表并找不到与版本字符串相关的任何内容。但是我知道它存储在.exe中。

我在谷歌上搜索了很多,但找不到任何真正值得的东西。我尝试使用 GetFileVersionInfo,但我似乎得到了一些随机的东西。

有什么想法吗?

编辑

叹息....

原来 pywin32 并不总是安装在所有机器上。有谁知道是否可以通过 ctypes 做同样的事情?

这也仅适用于 Windows。

【问题讨论】:

    标签: python winapi


    【解决方案1】:

    有一个名为“strings”的 gnu linux 实用程序可以打印任何文件(二进制或非二进制)中的可打印字符,尝试使用它并查找类似模式的版本号

    在 windows 上,你可以在这里获取字符串 http://unxutils.sourceforge.net/

    【讨论】:

      【解决方案2】:

      如果您不想使用 pywin32 执行此操作,您当然可以使用 ctypes 执行此操作。

      诀窍在于解码返回的愚蠢文件版本结构。

      有一个old mailing list post 正在按照您的要求进行操作。不幸的是,我现在没有一个方便的 windows 框来自己测试这个。但如果它不起作用,它至少应该给你一个好的开始。

      这是代码,以防那些 2006 年的档案在某个时候消失:

      import array
      from ctypes import *
      
      def get_file_info(filename, info):
          """
          Extract information from a file.
          """
          # Get size needed for buffer (0 if no info)
          size = windll.version.GetFileVersionInfoSizeA(filename, None)
          # If no info in file -> empty string
          if not size:
              return ''
          # Create buffer
          res = create_string_buffer(size)
          # Load file informations into buffer res
          windll.version.GetFileVersionInfoA(filename, None, size, res)
          r = c_uint()
          l = c_uint()
          # Look for codepages
          windll.version.VerQueryValueA(res, '\\VarFileInfo\\Translation',
                                        byref(r), byref(l))
          # If no codepage -> empty string
          if not l.value:
              return ''
          # Take the first codepage (what else ?)
          codepages = array.array('H', string_at(r.value, l.value))
          codepage = tuple(codepages[:2].tolist())
          # Extract information
          windll.version.VerQueryValueA(res, ('\\StringFileInfo\\%04x%04x\\'
      + info) % codepage, byref(r), byref(l))
          return string_at(r.value, l.value)
      
      print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')
      

      --

      好的 - 回到 windows 框附近。现在已经实际尝试过此代码。 “为我工作”。

      >>> print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')
      6.1.7600.16385 (win7_rtm.090713-1255)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2011-03-21
        • 1970-01-01
        相关资源
        最近更新 更多