Is there an API or other way to get the version of an executable?
Printable View
Is there an API or other way to get the version of an executable?
From www.vbapi.com
Code:' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public 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
Public Declare Function GetFileVersionInfoSize Lib "version.dll" Alias _
"GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
Public Declare Function VerQueryValue Lib "version.dll" Alias "VerQueryValueA" (pBlock _
As Any, ByVal lpSubBlock As String, lplpBuffer As Long, puLen As Long) As Long
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 _
As Any, ByVal lpString2 As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Public Type VS_FIXEDFILEINFO
dwSignature As Long
dwStrucVersion As Long
dwFileVersionMS As Long
dwFileVersionLS As Long
dwProductVersionMS As Long
dwProductVersionLS As Long
dwFileFlagsMask As Long
dwFileFlags As Long
dwFileOS As Long
dwFileType As Long
dwFileSubtype As Long
dwFileDateMS As Long
dwFileDateLS As Long
End Type
Public Const VFT_APP = &H1
Public Const VFT_DLL = &H2
Public Const VFT_DRV = &H3
Public Const VFT_VXD = &H5
' *** Place the following function definitions inside a module. ***
' HIWORD and LOWORD are API macros defined below.
Public Function HIWORD (ByVal dwValue As Long) As Long
Dim hexstr As String
hexstr = Right("00000000" & Hex(dwValue), 8)
HIWORD = CLng("&H" & Left(hexstr, 4))
End Function
Public Function LOWORD (ByVal dwValue As Long) As Long
Dim hexstr As String
hexstr = Right("00000000" & Hex(dwValue), 8)
LOWORD = CLng("&H" & Right(hexstr, 4))
End Function
' This nifty subroutine swaps two byte values without needing a buffer variable.
' This technique, which uses Xor, works as long as the two values to be swapped are
' numeric and of the same data type (here, both Byte).
Public Sub SwapByte (byte1 As Byte, byte2 As Byte)
byte1 = byte1 Xor byte2
byte2 = byte1 Xor byte2
byte1 = byte1 Xor byte2
End Sub
' This function creates a hexadecimal string to represent a number, but it
' outputs a string of a fixed number of digits. Extra zeros are added to make
' the string the proper length. The "&H" prefix is not put into the string.
Public Function FixedHex (ByVal hexval As Long, ByVal nDigits As Long) As String
FixedHex = Right("00000000" & Hex(hexval), nDigits)
End Function
' *** Place the following code inside the form that has Command1 and Text1. ***
Private Sub Command1_Click()
Dim vffi As VS_FIXEDFILEINFO ' version info structure
Dim buffer() As Byte ' buffer for version info resource
Dim pData As Long ' pointer to version info data
Dim nDataLen As Long ' length of info pointed at by pData
Dim cpl(0 To 3) As Byte ' buffer for code page & language
Dim cplstr As String ' 8-digit hex string of cpl
Dim dispstr As String ' string used to display version information
Dim retval As Long ' generic return value
' First, get the size of the version info resource. If this function fails, then Text1
' identifies a file that isn't a 32-bit executable/DLL/etc.
nDataLen = GetFileVersionInfoSize(Text1.Text, pData)
If nDataLen = 0 Then
Debug.Print "Not a 32-bit executable!"
Exit Sub
End If
' Make the buffer large enough to hold the version info resource.
ReDim buffer(0 To nDataLen - 1) As Byte
' Get the version information resource.
retval = GetFileVersionInfo(Text1.Text, 0, nDataLen, buffer(0))
' Get a pointer to a structure that holds a bunch of data.
retval = VerQueryValue(buffer(0), "\", pData, nDataLen)
' Copy that structure into the one we can access.
CopyMemory vffi, ByVal pData, nDataLen
' Display the full version number of the file.
dispstr = Trim(Str(HIWORD(vffi.dwFileVersionMS))) & "." & _
Trim(Str(LOWORD(vffi.dwFileVersionMS))) & "." & _
Trim(Str(HIWORD(vffi.dwFileVersionLS))) & "." & _
Trim(Str(LOWORD(vffi.dwFileVersionLS)))
Debug.Print "Version Number: "; dispstr
' Display the type of file it is (i.e., executable, DLL, etc.).
Select Case vffi.dwFileType
Case VFT_APP
dispstr = "Application"
Case VFT_DLL
dispstr = "Dynamic Link Library (DLL)"
Case VFT_DRV
dispstr = "Device Driver"
Case VFT_VXD
dispstr = "Virtual Device Driver"
Case Else
dispstr = "Unknown"
End Select
Debug.Print "File Type: "; dispstr
' Before reading any strings out of the resource, we must first determine the code page
' and language. The code to get this information follows.
retval = VerQueryValue(buffer(0), "\VarFileInfo\Translation", pData, nDataLen)
' Copy that informtion into the byte array.
CopyMemory cpl(0), ByVal pData, 4
' It is necessary to swap the first two bytes, as well as the last two bytes.
SwapByte cpl(0), cpl(1)
SwapByte cpl(2), cpl(3)
' Convert those four bytes into a 8-digit hexadecimal string.
cplstr = FixedHex(cpl(0), 2) & FixedHex(cpl(1), 2) & FixedHex(cpl(2), 2) & _
FixedHex(cpl(3), 2)
' cplstr now represents the code page and language to read strings as.
' Read the copyright information from the version info resource.
retval = VerQueryValue(buffer(0), "\StringFileInfo\" & cplstr & "\LegalCopyright", _
pData, nDataLen)
' Copy that data into a string for display.
dispstr = Space(nDataLen)
retval = lstrcpy(dispstr, pData)
' Display the result.
Debug.Print "Copyright Info: "; dispstr
' Similarly, read a description of the file and display it.
retval = VerQueryValue(buffer(0), "\StringFileInfo\" & cplstr & "\FileDescription", _
pData, nDataLen)
dispstr = Space(nDataLen)
retval = lstrcpy(dispstr, pData)
Debug.Print "File Description: "; dispstr
End Sub
Try this:
Code:'Submitted on: 4/13/1999
'By: Riaan Aspeling
'Level: Not Given
'User Rating: By 5 Users
'Compatibility:VB 5.0/6.0
'Description: Retrieve the version of a file (EXE/DLL etc).
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 Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" (dest As Any, ByVal Source As _
Long, ByVal Length As Long)
Private Type VS_FIXEDFILEINFO
dwSignature As Long
dwStrucVersionl As Integer ' e.g. = &h0000 = 0
dwStrucVersionh As Integer ' e.g. = &h0042 = .42
dwFileVersionMSl As Integer ' e.g. = &h0003 = 3
dwFileVersionMSh As Integer ' e.g. = &h0075 = .75
dwFileVersionLSl As Integer ' e.g. = &h0000 = 0
dwFileVersionLSh As Integer ' e.g. = &h0031 = .31
dwProductVersionMSl As Integer ' e.g. = &h0003 = 3
dwProductVersionMSh As Integer ' e.g. = &h0010 = .1
dwProductVersionLSl As Integer ' e.g. = &h0000 = 0
dwProductVersionLSh As Integer ' e.g. = &h0031 = .31
dwFileFlagsMask As Long ' = &h3F for version "0.42"
dwFileFlags As Long ' e.g. VFF_DEBUG or VFF_PRERELEASE
dwFileOS As Long ' e.g. VOS_DOS_WINDOWS16
dwFileType As Long ' e.g. VFT_DRIVER
dwFileSubtype As Long ' e.g. VFT2_DRV_KEYBOARD
dwFileDateMS As Long ' e.g. 0
dwFileDateLS As Long ' e.g. 0
End Type
Private Function CheckFileVersion(FilenameAndPath As Variant) As Variant
On Error GoTo HandelCheckFileVersionError
Dim lDummy As Long, lsize As Long, rc As Long
Dim lVerbufferLen As Long, lVerPointer As Long
Dim sBuffer() As Byte
Dim udtVerBuffer As VS_FIXEDFILEINFO
Dim ProdVer As String
lsize = GetFileVersionInfoSize(FilenameAndPath, lDummy)
If lsize < 1 Then Exit Function
ReDim sBuffer(lsize)
rc = GetFileVersionInfo(FilenameAndPath, 0&, lsize, sBuffer(0))
rc = VerQueryValue(sBuffer(0), "\", lVerPointer, lVerbufferLen)
MoveMemory udtVerBuffer, lVerPointer, Len(udtVerBuffer)
'**** Determine Product Version number *
' ***
ProdVer = Format$(udtVerBuffer.dwProductVersionMSh) & "." & Format$(udtVerBuffer.dwProductVersionMSl)
'Full number:
'ProdVer = Format$(udtVerBuffer.dwProductVersionMSh) & "." & Format$(udtVerBuffer.dwProductVersionMSl) & "." & Format$(udtVerBuffer.dwFileVersionLSh) & Format$(udtVerBuffer.dwFileVersionLSl)
CheckFileVersion = ProdVer
Exit Function
HandelCheckFileVersionError:
CheckFileVersion = "N/A"
Exit Function
End Function
Private Sub Command1_Click()
Msgbox CheckFileVersion("C:\Windows\Notepad.exe")
End Sub
Thanks alot for the code guys!
gwdash, how did your post get before mine? When I posted, I was the only one that posted on here :rolleyes:.
maybe it's a glitch, or maybe you had not refreshed in 7 minutes. Who knows? I posted at the time said though.
I tested both codes on my computer and had problems using the code of vbapi.
When you run the code several times you'll get out of memory and vb crashes. :mad:
I'm not so brilliant in using API but I think the problem is that copying the memory without freeing it after usage brings vb into trouble...
Matthew's code works fine (after 10000-tests) :)