I want to retrieve file information. I have some code written in VB3
and it works fine. Is there anybody out there who can help me
translate this to VB6?
Thanks
Avi Manor
This is the code in VB3:
Here are some lines that retrieve file information:
'Debug.Print GetFileVersion((Found_List.Text), "CompanyName")
'Debug.Print GetFileVersion((Found_List.Text), "FileDescription")
'Debug.Print GetFileVersion((Found_List.Text), "FileVersion")
Version_Label.Caption = GetFileVersion(Search_File.Path + "\" +
Search_File.FileName, "ProductName")
Version_Label.Caption = Version_Label.Caption + " Ver. " +
GetFileVersion(Search_File.Path + "\" + Search_File.FileName,
"ProductVersion")
Here are some API declarations:
Declare Function GetFileVersionInfoSize Lib "ver.dll" (ByVal
lpszFileName As String, lpdwHandle As Long) As Long
Declare Function GetFileVersionInfo Lib "ver.dll" (ByVal lpszFileName
As String, ByVal lpdwHandle As Long, ByVal cbbuf As Long, ByVal
lpvdata As String) As Integer
Declare Function VerQueryValue Lib "ver.dll" (ByVal lpvBlock As
String, ByVal lpszSubBlock As String, lplpBuffer As Long, lpcb As
Integer) As Integer
Declare Function lstrcpyn Lib "Kernel" (ByVal lpszString1 As Any,
ByVal lpszString2 As Long, ByVal cChars As Integer) As Long
Declare Function DiskSpaceFree Lib "SETUPKIT.DLL" () As Long
This function is dowing the dirty work:
Function GetFileVersion (FileToCheck As String, xi_subver As String)
As String
' FileToCheck is the file name and path
' xi_subver indicates the required information
Dim lpdwHandle&, VersionInfoSize&, lplpBuffer&, stringcopy&,
TransValue&
Dim lpvdata$, TransString$, fixedstr$, char$, nextchar$,
TransTable$
Dim VersionInfo%, lpcb%, ptrFixed%, ptrString%
Dim i As Integer
On Error Resume Next
VersionInfoSize& = GetFileVersionInfoSize(FileToCheck, lpdwHandle&)
If VersionInfoSize& = 0 Then
GetFileVersion = ""
Exit Function
End If
lpvdata$ = String(VersionInfoSize&, Chr$(0))
VersionInfo% = GetFileVersionInfo(FileToCheck, lpdwHandle&,
VersionInfoSize&, lpvdata$)
ptrFixed% = VerQueryValue(lpvdata$, "\FILEVERSION", lplpBuffer&,
lpcb%)
If ptrFixed% = 0 Then
' Take a shot with the hardcoded TransString
TransString$ = "040904E4"
ptrString% = VerQueryValue(lpvdata$, "\StringFileInfo\" &
TransString$ & "\CompanyName", lplpBuffer&, lpcb%)
If ptrString% <> 0 Then GoTo GetValues
ptrFixed% = VerQueryValue(lpvdata$, "\", lplpBuffer&, lpcb%)
If ptrFixed% = 0 Then
GetFileVersion = ""
Exit Function
Else
TransString$ = ""
fixedstr$ = String(lpcb% + 1, Chr(0))
stringcopy& = lstrcpyn(fixedstr$, lplpBuffer&, lpcb% + 1)
For i = lpcb% To 1 Step -1
char$ = Hex(Asc(Mid(fixedstr$, i, 1)))
If Len(char$) = 1 Then
char$ = "0" + char$
End If
TransString$ = TransString$ + char$
If Len(TransString$ & nextchar$) Mod 8 = 0 Then
TransString$ = "&H" & TransString$
TransValue& = Val(TransString$)
TransString$ = ""
End If
Next i
End If
End If
TransTable$ = String(lpcb% + 1, Chr(0))
TransString$ = String(0, Chr(0))
stringcopy& = lstrcpyn(TransTable$, lplpBuffer&, lpcb% + 1)
For i = 1 To lpcb%
char$ = Hex(Asc(Mid(TransTable$, i, 1)))
If Len(char$) = 1 Then
char$ = "0" + char$
End If
If Len(TransString$ & nextchar$) Mod 4 = 0 Then
nextchar$ = char$
Else
TransString$ = TransString$ + char$ + nextchar$
nextchar$ = ""
char$ = ""
End If
Next i
GetValues:
'ptrString% = VerQueryValue(lpvdata$, "\StringFileInfo\" &
TransString$ & "\FileVersion", lplpBuffer&, lpcb%)
ptrString% = VerQueryValue(lpvdata$, "\StringFileInfo\" &
TransString$ & "\" & xi_subver, lplpBuffer&, lpcb%)
If ptrString% = 1 Then
TransTable$ = String(lpcb%, Chr(0))
stringcopy& = lstrcpyn(TransTable$, lplpBuffer&, lpcb% + 1)
GetFileVersion = TransTable$
Else
GetFileVersion = ""
End If
End Function