VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 获得版本信息
发表评论(0)作者:Karl Moore, 平台:VB6.0+Win98, 阅读:9750, 日期:2001-04-24
Get Version Information

Should your code replace that old DLL with a newer copy? Is your program compatible with the installed version of Office? Let your application make intelligent decisions based upon file version information, retrievable with this handy copy-and-paste code snippet!

To use it, simply call the GetVersionInfo function, passing in your filename. It will return either the version number or 注释:N/A注释: if the file either doesn注释:t exist or contains no version resource.

This code works by packaging numerous API calls and various result-parsing techniques together into one neat function.

Usage

x = GetVersionInfo("c:\program files\microsoft office\office\MSO9.DLL")Code

Private Type VS_FIXEDFILEINFO
    dwSignature As Long
    dwStrucVersion As Long
    dwFileVersionMSl As Integer
    dwFileVersionMSh As Integer
    dwFileVersionLSl As Integer
    dwFileVersionLSh As Integer
    dwProductVersionMSl As Integer
    dwProductVersionMSh As Integer
    dwProductVersionLSl As Integer
    dwProductVersionLSh As Integer
    dwFileFlagsMask As Long
    dwFileFlags As Long
    dwFileOS As Long
    dwFileType As Long
    dwFileSubtype As Long
    dwFileDateMS As Long
    dwFileDateLS As Long
End Type

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 Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" (dest As Any, src As _
    Long, ByVal length 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

Public Function GetVersionInfo(ByVal sFile As _
    String) As String

    Dim lDummy As Long
    Dim sBuffer() As Byte
    Dim lBufferLen As Long, lVerPointer As Long
    Dim lVerBufferLen As Long
    Dim udtVerBuffer As VS_FIXEDFILEINFO
    
    注释:Default return value
    GetVersionInfo = "N/A"
    
    注释:Attempt to retrieve version resource
    lBufferLen = GetFileVersionInfoSize(sFile, _
    lDummy)
    
    If lBufferLen > 0 Then
        
        ReDim sBuffer(lBufferLen)
        
        If GetFileVersionInfo(sFile, 0&, _
            lBufferLen, sBuffer(0)) <> 0 Then
            
            If VerQueryValue(sBuffer(0), _
                "\", lVerPointer, lVerBufferLen) _
                <> 0 Then
        
                CopyMemory udtVerBuffer, ByVal _
                    lVerPointer, Len(udtVerBuffer)
                    
                With udtVerBuffer
                    GetVersionInfo = _
                        .dwFileVersionMSh & "." & _
                        .dwFileVersionMSl & "." & _
                        .dwFileVersionLSh & "." & _
                        .dwFileVersionLSl
                End With
        
            End If
        End If
    End If

End FunctionTip by Karl Moore