471,122 Members | 1,114 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

Crashing getfileversioninfo function, what did I do wrong ?

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,
Fabster
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

Public Type FILEPROPERTIE
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
FILEPROPERTIE
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,
bytBuffer(0))
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
code
'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:
'04------ = SUBLANG_ENGLISH_USA
'--09---- = LANG_ENGLISH
' ----04E4 = 1252 = Codepage for Windows:Multilingual
Do While Len(strLangCharset) < 8
strLangCharset = "0" & strLangCharset
Loop
' 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 & "\" &
strVersionInfo(intTemp)
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
Else
' 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
0 3647

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by xytho33 | last post: by
2 posts views Thread by xytho33 | last post: by
2 posts views Thread by L. Shestov | last post: by
reply views Thread by Henry Hank | last post: by
14 posts views Thread by Java and Swing | last post: by
34 posts views Thread by richard | last post: by
3 posts views Thread by Shawn August | last post: by
2 posts views Thread by =?Utf-8?B?QW5uZXh4eHh4eHg=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.