Important alert: (current site time 7/16/2013 3:56:09 AM EDT)
 

VB icon

Get Version Number for EXE, DLL or OCX files

Email
Submitted on: 12/17/1999
By: Serge 
Level: Advanced
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 63598
 
     This function will retrieve the version number, product name, original program name (like if you right click on the EXE file and select properties, then select Version tab, it shows you all that information) etc
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Get Version Number for EXE, DLL or OCX files
'**************************************
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 Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Public Type FILEINFO
CompanyName As String
FileDescription As String
FileVersion As String
InternalNameAs String
LegalCopyright As String
OriginalFileName As String
ProductName As String
ProductVersion As String
End Type
Public Enum VerisonReturnValue
eOK = 1
eNoVersion = 2
End Enum
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Get Version Number for EXE, DLL or OCX files
' Description:This function will retrieve the version number, product name, original program name (like if you right click on the EXE file and select properties, then select Version tab, it shows you all that information) etc
' By: Serge
'
' Returns:FileInfo structure
'
' Assumes:Label (named Label1 and make it wide enough, also increase the height of the label to have size of the form), Common Dilaog Box (CommonDialog1) and a Command Button (Command1)
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4976&lngWId=1'for details.'**************************************

Public Function GetFileVersionInformation(ByRef pstrFieName As String, ByRef tFileInfo As FILEINFO) As VerisonReturnValue
Dim lBufferLen As Long, lDummy As Long
Dim sBuffer() As Byte
Dim lVerPointer As Long
Dim lRet As Long
Dim Lang_Charset_String As String
Dim HexNumber As Long
Dim i As Integer
Dim strTemp As String
'Clear the Buffer tFileInfo
tFileInfo.CompanyName = ""
tFileInfo.FileDescription = ""
tFileInfo.FileVersion = ""
tFileInfo.InternalName = ""
tFileInfo.LegalCopyright = ""
tFileInfo.OriginalFileName = ""
tFileInfo.ProductName = ""
tFileInfo.ProductVersion = ""
lBufferLen = GetFileVersionInfoSize(pstrFieName, lDummy)
If lBufferLen < 1 Then
GetFileVersionInformation = eNoVersion
Exit Function
End If
ReDim sBuffer(lBufferLen)
lRet = GetFileVersionInfo(pstrFieName, 0&, lBufferLen, sBuffer(0))
If lRet = 0 Then
GetFileVersionInformation = eNoVersion
Exit Function
End If
lRet = VerQueryValue(sBuffer(0), "\VarFileInfo\Translation", lVerPointer, lBufferLen)
If lRet = 0 Then
GetFileVersionInformation = eNoVersion
Exit Function
End If
Dim bytebuffer(255) As Byte
MoveMemory bytebuffer(0), lVerPointer, lBufferLen
HexNumber = bytebuffer(2) + bytebuffer(3) * &H100 + bytebuffer(0) * &H10000 + bytebuffer(1) * &H1000000
Lang_Charset_String = Hex(HexNumber)
'Pull it all apart:
'04------= SUBLANG_ENGLISH_USA
'--09----= LANG_ENGLISH
' ----04E4 = 1252 = Codepage for Windows:Multilingual
Do While Len(Lang_Charset_String) < 8
Lang_Charset_String = "0" & Lang_Charset_String
Loop
Dim strVersionInfo(7) As String
strVersionInfo(0) = "CompanyName"
strVersionInfo(1) = "FileDescription"
strVersionInfo(2) = "FileVersion"
strVersionInfo(3) = "InternalName"
strVersionInfo(4) = "LegalCopyright"
strVersionInfo(5) = "OriginalFileName"
strVersionInfo(6) = "ProductName"
strVersionInfo(7) = "ProductVersion"
Dim buffer As String
For i = 0 To 7
buffer = String(255, 0)
strTemp = "\StringFileInfo\" & Lang_Charset_String _
& "\" & strVersionInfo(i)
lRet = VerQueryValue(sBuffer(0), strTemp, _
lVerPointer, lBufferLen)
If lRet = 0 Then
GetFileVersionInformation = eNoVersion
Exit Function
End If
lstrcpy buffer, lVerPointer
buffer = Mid$(buffer, 1, InStr(buffer, vbNullChar) - 1)
Select Case i
Case 0
tFileInfo.CompanyName = buffer
Case 1
tFileInfo.FileDescription = buffer
Case 2
tFileInfo.FileVersion = buffer
Case 3
tFileInfo.InternalName = buffer
Case 4
tFileInfo.LegalCopyright = buffer
Case 5
tFileInfo.OriginalFileName = buffer
Case 6
tFileInfo.ProductName = buffer
Case 7
tFileInfo.ProductVersion = buffer
End Select
Next i
GetFileVersionInformation = eOK
End Function
'-----------
Private Sub Command1_Click()
Dim strFile As String
Dim udtFileInfo As FILEINFO
On Error Resume Next
With CommonDialog1
.Filter = "All Files (*.*)|*.*"
.ShowOpen
strFile = .FileName
If Err.Number = cdlCancel Or strFile = "" Then Exit Sub
End With
If GetFileVersionInformation(strFile, udtFileInfo) = eNoVersion Then
MsgBox "No version available for this file", vbInformation
Exit Sub
End If
Label1 = "Company Name: " & udtFileInfo.CompanyName & vbCrLf
Label1 = Label1 & "File Description:" & udtFileInfo.FileDescription & vbCrLf
Label1 = Label1 & "File Version:" & udtFileInfo.FileVersion & vbCrLf
Label1 = Label1 & "Internal Name: " & udtFileInfo.InternalName & vbCrLf
Label1 = Label1 & "Legal Copyright: " & udtFileInfo.LegalCopyright & vbCrLf
Label1 = Label1 & "Original FileName:" & udtFileInfo.OriginalFileName & vbCrLf
Label1 = Label1 & "Product Name:" & udtFileInfo.ProductName & vbCrLf
Label1 = Label1 & "Product Version: " & udtFileInfo.ProductVersion & vbCrLf
End Sub


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/17/1999 9:33:58 AMdon

This is a great code. I've been trying to achieve this for ages. Thank you very much.
(If this comment was disrespectful, please report it.)

 
12/17/1999 3:58:57 PMAlex

I agree. This is a great code. I've asked many people how to do that, but no one actually gave me the answer. You're great programmer Serge. Keep it up.
(If this comment was disrespectful, please report it.)

 
1/2/2000 12:14:56 PMThe_Lung

If no one paid attention my code can manually read and write this info from executable type files with out the help of the api.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4224
(If this comment was disrespectful, please report it.)

 
1/27/2000 9:19:54 AMSteven Lee

I think the codes are very useful but I won't place it in "advanced" category.
(If this comment was disrespectful, please report it.)

 
9/4/2000 9:25:28 AMMug

Guys wake up!!! Have you ever heard about the MSDN library?????
Go there and you will find exact copy of the code above. Shame of you!
(If this comment was disrespectful, please report it.)

 
1/27/2004 8:50:46 PM

It works for most standard exe's, but dosnt work for any ive created in vb. any ideas.? just get message no version info available for this file.
(If this comment was disrespectful, please report it.)

 
6/5/2004 9:48:46 AM

better if it would have been complied in a project and submitted as an zip on PSC... but good code anyway? :)
(If this comment was disrespectful, please report it.)

 
9/11/2004 9:33:02 AM

Very GOOD!! Helped me a lot!!
Thanks & Regards
(If this comment was disrespectful, please report it.)

 
11/25/2004 1:06:59 PM

Hi Serge,
your code is somthing i'm looking for.
As i'm rather new in vb6 programming, can you please tell me more about what should be place where in my app. Wich part goes into a module, wich part into a form etc...
Thanks in advance.
(If this comment was disrespectful, please report it.)

 
11/25/2004 1:08:24 PM

Serge, can you please tell me what should be placed where in my app, i'm rather new in using api calls.
(If this comment was disrespectful, please report it.)

 
3/24/2005 1:44:00 AM

Hi Serge,
your code is somthing i'm
looking for.
As i'm rather new in vb6
programming, can you please tell me
more about what should be place where
in my app. Wich part goes into a
module, wich part into a form
etc...
Thanks in advance.

(If this comment was disrespectful, please report it.)

 
11/23/2010 10:34:52 AMAvinash Sureka

Wonderful utility for know the exe version....may god bless you....
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.