玩酷之家

 找回密码
 立即注册
查看: 76|回复: 0

VB API获取应用程序文件版本号

[复制链接]
发表于 2021-3-12 23:12:10 | 显示全部楼层 |阅读模式

使用GetFileVersionInfo获取文件版本号,可获取文件版本和产品版本。调用函数:MsgBox GetVerInfo("C:\Windows\notepad.exe")
  1. 'Module
  2. Private Type VS_FIXEDFILEINFO
  3.    dwSignature As Long
  4.    dwStrucVersionl As Integer     '  e.g. = &h0000 = 0
  5.    dwStrucVersionh As Integer     '  e.g. = &h0042 = .42
  6.    dwFileVersionMSl As Integer    '  e.g. = &h0003 = 3
  7.    dwFileVersionMSh As Integer    '  e.g. = &h0075 = .75
  8.    dwFileVersionLSl As Integer    '  e.g. = &h0000 = 0
  9.    dwFileVersionLSh As Integer    '  e.g. = &h0031 = .31
  10.    dwProductVersionMSl As Integer '  e.g. = &h0003 = 3
  11.    dwProductVersionMSh As Integer '  e.g. = &h0010 = .1
  12.    dwProductVersionLSl As Integer '  e.g. = &h0000 = 0
  13.    dwProductVersionLSh As Integer '  e.g. = &h0031 = .31
  14. End Type


  15. Private Declare Function GetFileVersionInfo Lib "Version.dll" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwhandle As Long, ByVal dwlen As Long, lpData As Any) As Long
  16. Private Declare Function GetFileVersionInfoSize Lib "Version.dll" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
  17. Private Declare Function VerQueryValue Lib "Version.dll" Alias "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Any, puLen As Long) As Long
  18. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal Source As Long, ByVal length As Long)



  19. Dim FileVer As String, ProdVer As String


  20. Public Function GetVerInfo(FilePath As String)
  21.    Dim rc As Long, lDummy As Long, sBuffer() As Byte
  22.    Dim lBufferLen As Long, lVerPointer As Long, udtVerBuffer As VS_FIXEDFILEINFO
  23.    Dim lVerbufferLen As Long
  24.    
  25.    lBufferLen = GetFileVersionInfoSize(FilePath, lDummy)

  26.    If lBufferLen < 1 Then
  27.       'MsgBox "No Version Info available!"
  28.       Exit Function
  29.    End If
  30.    '**** Store info to udtVerBuffer struct ****
  31.    ReDim sBuffer(lBufferLen)
  32.    rc = GetFileVersionInfo(FilePath, 0&, lBufferLen, sBuffer(0))
  33.    rc = VerQueryValue(sBuffer(0), "", lVerPointer, lVerbufferLen)
  34.    MoveMemory udtVerBuffer, lVerPointer, Len(udtVerBuffer)
  35.    '**** Determine Structure Version number - NOT USED ****
  36.    StrucVer = Format$(udtVerBuffer.dwStrucVersionh) & "." & Format$(udtVerBuffer.dwStrucVersionl)
  37.    '**** Determine File Version number ****
  38.    FileVer = Format$(udtVerBuffer.dwFileVersionMSh) & "." & Format$(udtVerBuffer.dwFileVersionMSl) & "." & Format$(udtVerBuffer.dwFileVersionLSh) & "." & Format$(udtVerBuffer.dwFileVersionLSl)
  39.    '**** Determine Product Version number ****
  40.    ProdVer = Format$(udtVerBuffer.dwProductVersionMSh) & "." & Format$(udtVerBuffer.dwProductVersionMSl) & "." & Format$(udtVerBuffer.dwProductVersionLSh) & "." & Format$(udtVerBuffer.dwProductVersionLSl)
  41.    '**** Determine Boolean attributes of File ****
  42.    GetVerInfo = ProdVer
  43. End Function
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号 )

GMT+8, 2021-4-22 11:42 , Processed in 1.076402 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表