By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Crashing getfileversioninfo function, what did I do wrong ?

P: n/a
Below is a code part I got somewhere and works fine for retrieving a
version from a single file. Now I want to retrieve all .dll, .ocx and
..exe file versions from my harddisk in all directory's. I generate a
array of all those files and then in a loop request the version for
each of those files.

Now it randomly crashes with a error that it cannot write to memory at
location xxxxxx. I cannot find the error and adding 'on error goto
blabla' doesnt help at all. Whats wrong on the code below ?

Many thanks for looking at it,
Option Explicit

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
Declare Function GetFileVersionInfoSize Lib "Version.dll" Alias
"GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle
As Long) As Long
Declare Function VerQueryValue Lib "Version.dll" Alias
"VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String,
lplpBuffer As Any, puLen As Long) As Long
Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As
Any, ByVal Source As Long, ByVal Length As Long)
Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal
lpString1 As String, ByVal lpString2 As Long) As Long

CompanyName As String
FileDescription As String
FileVersion As String
InternalName As String
LegalCopyright As String
OrigionalFileName As String
ProductName As String
ProductVersion As String
End Type

Public Function fileinfo(Optional ByVal PathWithFilename As String) As
Dim lngBufferlen As Long
Dim lngDummy As Long
Dim lngRc As Long
Dim lngVerPointer As Long
Dim lngHexNumber As Long
Dim bytBuffer() As Byte
Dim bytBuff(255) As Byte
Dim strBuffer As String
Dim strLangCharset As String
Dim strVersionInfo(7) As String
Dim strTemp As String
Dim intTemp As Integer

On Error GoTo 0
' size
lngBufferlen = GetFileVersionInfoSize(PathWithFilename, lngDummy)
If lngBufferlen > 0 Then
ReDim bytBuffer(lngBufferlen)
lngRc = GetFileVersionInfo(PathWithFilename, 0&, lngBufferlen,
If lngRc <> 0 Then
lngRc = VerQueryValue(bytBuffer(0),
"\VarFileInfo\Translation", lngVerPointer, lngBufferlen)
If lngRc <> 0 Then
'lngVerPointer is a pointer to four 4 bytes of Hex number,
'first two bytes are language id, and last two bytes are
'page. However, strLangCharset needs a string of
'4 hex digits, the first two characters correspond to the
'language id and last two the last two character correspond
'to the code page id.
MoveMemory bytBuff(0), lngVerPointer, lngBufferlen
lngHexNumber = bytBuff(2) + bytBuff(3) * &H100 + bytBuff(0)
* &H10000 + bytBuff(1) * &H1000000
strLangCharset = Hex(lngHexNumber)
'now we change the order of the language id and code page
'and convert it into a string representation.
'For example, it may look like 040904E4
'Or to pull it all apart:
'--09---- = LANG_ENGLISH
' ----04E4 = 1252 = Codepage for Windows:Multilingual
Do While Len(strLangCharset) < 8
strLangCharset = "0" & strLangCharset
' assign propertienames
strVersionInfo(0) = "CompanyName"
strVersionInfo(1) = "FileDescription"
strVersionInfo(2) = "FileVersion"
strVersionInfo(3) = "InternalName"
strVersionInfo(4) = "LegalCopyright"
strVersionInfo(5) = "OriginalFileName"
strVersionInfo(6) = "ProductName"
strVersionInfo(7) = "ProductVersion"
' loop and get fileproperties
For intTemp = 0 To 7
strBuffer = String$(255, 0)
strTemp = "\StringFileInfo\" & strLangCharset & "\" &
lngRc = VerQueryValue(bytBuffer(0), strTemp,
lngVerPointer, lngBufferlen)
If lngRc <> 0 Then
' get and format data
lstrcpy strBuffer, lngVerPointer
strBuffer = Mid$(strBuffer, 1, InStr(strBuffer, Chr(0))
- 1)
strVersionInfo(intTemp) = strBuffer
' property not found
strVersionInfo(intTemp) = "?"
End If
Next intTemp
End If
End If
End If
' assign array to user-defined-type
fileinfo.CompanyName = strVersionInfo(0)
fileinfo.FileDescription = strVersionInfo(1)
fileinfo.FileVersion = strVersionInfo(2)
fileinfo.InternalName = strVersionInfo(3)
fileinfo.LegalCopyright = strVersionInfo(4)
fileinfo.OrigionalFileName = strVersionInfo(5)
fileinfo.ProductName = strVersionInfo(6)
fileinfo.ProductVersion = strVersionInfo(7)
End Function
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.