
Error retrieving file version information using "VerQueryValue"
Quote:
snip......
> However, at the point where VB calls the VerQueryValue function, I receive
> following error message
more snip.....
-----
Phillipe: Without seeing your code it's hard to debug it. Since it takes
more comments than code to use these functions, I'm including a working
version here that you can use to compare with your trouble code, or just use
this if it suits your needs. This code should run on all 32bit flavors of
VB.
Keep Smilin'
Ed Stegman
-------
Option Explicit
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
Private Declare Function GetFileVersionInfoSize Lib "Version.dll" Alias
"GetFileVersionInfoSizeA" _
(ByVal lptstrFilename As String, lpdwHandle As Long) As Long
Private Declare Function VerQueryValue Lib "Version.dll" Alias
"VerQueryValueA" _
(pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Any, puLen As
Long) As Long
Private Type VS_FIXEDFILEINFO
dwSignature As Long
dwStrucVersionl As Integer
dwStrucVersionh As Integer
dwFileVersionMSl As Integer
dwFileVersionMSh As Integer
dwFileVersionLSl As Integer
dwFileVersionLSh As Integer
dwProductVersionMSl As Integer
dwProductVersionMSh As Integer
dwProductVersionLSl As Integer
dwProductVersionLSh As Integer
dwFileFlagsMask As Long
dwFileFlags As Long
dwFileOS As Long
dwFileType As Long
dwFileSubtype As Long
dwFileDateMS As Long
dwFileDateLS As Long
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(dest As Any, ByVal Source As Long, ByVal length As Long)
Public Function GetFileVersion(ByVal sFilename As String) As Variant
'Returns a Variant containing a zero based Variant array with 5 elements on
success.
'Array Elements:
'0 to 3 contain integers that can be easily used for comparisons
'0 - Major (..MSh) high word
'1 - Minor (...MSl) low word
'2 - Not used by VB (...LSh) high word
'3 -Build (...LSl) low word
'4 contains a formatted string for displaying the version info.
'string format: 0.00.00
'Returns a string error message on trapped error.
'String format: <err num>, <source>,<description>
'Usage:
'' Dim vFileInfo As Variant
'' Const FILE_INFO_STR& = 4
'' vFileInfo = GetFileVersion(txtFilePath.Text)
'' If IsArray(vFileInfo) Then 'Success !
'' txtVersion.Text = vFileInfo(FILE_INFO_STR)
'' If (vFileInfo(0) < 7) And (vFileInfo(1) < 3) Then MsgBox "You need to
upgrade." _
'' & vbCrLf & _
'' "This version is mostly hype and should have stayed Beta for another
year."
'' Else 'failed
'' MsgBox FormatErrStr(vFileInfo)
'' End If
Dim lRet As Long
Dim lUseless As Long 'hehehe This is a gem!! Maybe used by Windows5000 ?
Dim abBuffer() As Byte
Dim lBufferLen As Long
Dim lpVersion As Long
Dim tagFixedFileInfo As VS_FIXEDFILEINFO
Dim lFixedFileInfoLen As Long
Dim avRet(0 To 4) As Variant
On Error GoTo EH
' Get the size needed for the buffer
lBufferLen = GetFileVersionInfoSize(sFilename, lUseless)
If lBufferLen >= 1 Then
'Success, size the buffer
ReDim abBuffer(lBufferLen) As Byte
'Get the version resource
lRet = GetFileVersionInfo(sFilename, 0&, lBufferLen, abBuffer(0))
' Now for some contortions that are anything but intuitive
lRet = VerQueryValue(abBuffer(0), "\", lpVersion, lFixedFileInfoLen)
CopyMemory tagFixedFileInfo, lpVersion, Len(tagFixedFileInfo)
'move it into our returning array
With tagFixedFileInfo
avRet(0) = .dwFileVersionMSh 'Major
avRet(1) = .dwFileVersionMSl 'Minor
avRet(2) = .dwFileVersionLSh 'NOTE: Not used by VB.
avRet(3) = .dwProductVersionLSl 'Build #
'format as 0.00.0000
avRet(4) = Format$(.dwFileVersionMSh, "0") & "." & _
Format$(.dwFileVersionMSl, "00") & "." & _
Format$(.dwFileVersionLSl, "00")
' NOTE: VB doesn't put anything here >>>> .dwFileVersionLSh
End With
GetFileVersion = avRet()
Else 'failed
GetFileVersion = "ERR_FAILED_GET_VERSION, GetFileVersion, Failed to find
version info for " & sFilename
End If
ExitNow:
Err.Clear
Exit Function
EH:
GetFileVersion = Err.Number & "," & "GetFileVersion:=>" & Err.Source & ","
& Err.Description
Resume ExitNow
End Function