Type FileInfo
wLength As Integer
wValueLength As Integer
szKey As String * 16
dwSignature As Long
dwStrucVersion As Long
dwFileVersionMS As Long
dwFileVersionLS As Long
End Type
' NOTE: The following Declare statements are case sensitive.
Declare Function GetFileVersionInfo& Lib "Version" _
Alias "GetFileVersionInfoA" (ByVal FileName$, _
ByVal dwHandle&, ByVal cbBuff&, ByVal lpvData$)
Declare Function GetFileVersionInfoSize& Lib "Version" Alias _
"GetFileVersionInfoSizeA" (ByVal FileName$, dwHandle&)
Declare Sub hmemcpy Lib "Kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, hpvSource As Any, ByVal cbBytes&)
Function LOWORD(x As Long) As Integer
LOWORD = x And &HFFFF&
' Low 16 bits contain Minor revision number.
End Function
Function HIWORD(x As Long) As Integer
HIWORD = x / &HFFFF&
' High 16 bits contain Major revision number.
End Function
'----------------------------------------
'以下为执行模块,在 VBE 界面按 F5 执行
'----------------------------------------
Function displayVersion()
Dim x As FileInfo
Dim FileVer As String
Dim FileName As String
Dim dwHandle&, BufSize&, lpvData$, R&
'*** Get Version Information If Available ****
FileVer = ""
'FileName = 路徑
FileName = Environ("windir") & "/system32/msjet40.dll"
BufSize& = GetFileVersionInfoSize(FileName, dwHandle&)
If BufSize& = 0 Then
MsgBox "Invalid File Name or no Version information available"
Exit Function
End If
lpvData$ = Space$(BufSize&)
R& = GetFileVersionInfo(FileName, dwHandle&, BufSize&, lpvData$)
hmemcpy x, ByVal lpvData$, Len(x)
'**** Parse File Version Number ****
FileVer = Trim$(Str$(HIWORD(x.dwFileVersionMS))) + "."
FileVer = FileVer + Trim$(Str$(LOWORD(x.dwFileVersionMS))) + "."
FileVer = FileVer + Trim$(Str$(HIWORD(x.dwFileVersionLS))) + "."
FileVer = FileVer + Trim$(Str$(LOWORD(x.dwFileVersionLS)))
MsgBox FileVer, 64, "Version of " & FileName
End Function
本文介绍了一个使用Visual Basic编写的函数,该函数可以从指定文件中提取版本信息。通过调用Windows API函数,如GetFileVersionInfo和GetFileVersionInfoSize,实现获取文件版本号的功能,并展示了如何解析这些版本号。

2592

被折叠的 条评论
为什么被折叠?



