Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting version number from windows exe

Status
Not open for further replies.

AndyWatt

Programmer
Oct 24, 2001
1,288
0
0
GB
Hi All,

I appreciate this is not necessarily the best place to ask this but I was wondering if anyone could tell me where in an exe file to find the version number (e.g. 1.2.3.4). I've compiled a simple .exe file with different version numbers, but I can't find the location by comparing the two files.


Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
You can use the FileSystemObject to get this information.

Code:
Dim FSO As Scripting.FileSystemObject
    
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFileVersion("C:\Path\ToFile\FileName.ext")
Set FSO = Nothing

You'll need to add a reference to "Microsoft Scripting Runtime" in order to use the FileSystemObject.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks for your reply gmmastros.

I'm actually trying to locate the offset(s) of the version info in the file header. I was hoping someone might know where/how it is stored.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Out of interest, why do you want the offsets rtaher than using the official methods?
 
You can retrieve the file version using version information API functions. The following code shows how to query the version information resource and extract the desired data.
___
[tt]
Option Explicit
Private Declare Function VerQueryValue Lib "version" Alias "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Long, puLen As Long) As Long
Private Declare Function GetFileVersionInfoSize Lib "version" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
Private Declare Function GetFileVersionInfo Lib "version" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwHandle As Long, ByVal dwLen As Long, lpData As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type VS_FIXEDFILEINFO
dwSignature As Long
dwStrucVersion As Long
dwFileVersionMS As Long '<-file version
dwFileVersionLS As Long '<-stored here
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

Function GetFileVersion(FileName As String) As String
Dim VerInfoSize As Long, Buffer() As Byte
Dim lpData As Long, cbData As Long
Dim vsffi As VS_FIXEDFILEINFO, Ver(3) As Integer

'Query the size of the version info data
VerInfoSize = GetFileVersionInfoSize(FileName, ByVal 0)
If VerInfoSize = 0 Then Exit Function

'Create the version info buffer and query the data
ReDim Buffer(0 To VerInfoSize - 1)
If GetFileVersionInfo(FileName, 0, VerInfoSize, Buffer(0)) = 0 Then Exit Function

'Query the VS_FIXEDFILEINFO structure
If VerQueryValue(Buffer(0), "\", lpData, cbData) = 0 Then Exit Function
CopyMemory vsffi, ByVal lpData, cbData

'copy the file version bytes to integer array for easy formatting
CopyMemory Ver(0), vsffi.dwFileVersionMS, 8

'return the file version
GetFileVersion = Ver(1) & "." & Ver(0) & "." & Ver(3) & "." & Ver(2)
End Function

Private Sub Form_Load()
MsgBox GetFileVersion("calc.exe")
End
End Sub[/tt]
___

As far as the result of the function is concerned, there is absolutely no difference in result of this function or the code provided by George.

The only difference is that his function uses FSO, and thats why its brief, whereas my function uses standard API functions to achieve the same.
 
Because PHP doesn't have a version number function, and I can't find a PHP code sample that shows how to extract the version data, so I have to write one, and I figured asking the clever folk in VB forum was a good place to start for finding out.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
There's no simple answer. If you have the MSDN help collection installed, enter this into internet explorer:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2005JAN.1033/debug/base/image_nt_headers_str.htm

This topic is the IMAGE_NT_HEADERS Win32 structure, which contains a pointer to a IMAGE_OPTIONAL_HEADER, which contains a Win32VersionValue, whose contents are reserved (but can probably be guessed).

You'll also need the information at:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2005JAN.1033/dndebug/html/msdn_peeringpe.htm

to read the file and find the offsets within the executable.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
I thought PHP supported COM on the Windows platform, in which case you should be able to use the FSO ...
 
Thanks very much for your replies, you've given me a good start!



Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top