Attribute VB_Name = "Win32API"
Option Explicit

Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Public Const SRCERASE = &H440328        ' (DWORD) dest = source AND (NOT dest )
Public Const SRCINVERT = &H660046       ' (DWORD) dest = source XOR dest
Public Const SRCPAINT = &HEE0086        ' (DWORD) dest = source OR dest

Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Public Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Public Declare Function InternetGetConnectedState Lib "wininet" (ByRef lFlag As Long, ByVal Reserved As Long) As Boolean

' File I/O ******************************************************************
Public Type VS_FIXEDFILEINFO
        dwSignature As Long
        dwStrucVersion As Long         '  e.g. 0x00000042 = "0.42"
        dwFileVersionMS As Long        '  e.g. 0x00030075 = "3.75"
        dwFileVersionLS As Long        '  e.g. 0x00000031 = "0.31"
        dwProductVersionMS As Long     '  e.g. 0x00030010 = "3.10"
        dwProductVersionLS As Long     '  e.g. 0x00000031 = "0.31"
        dwFileFlagsMask As Long        '  = 0x3F for version "0.42"
        dwFileFlags As Long            '  e.g. VFF_DEBUG Or VFF_PRERELEASE
        dwFileOS As Long               '  e.g. VOS_DOS_WINDOWS16
        dwFileType As Long             '  e.g. VFT_DRIVER
        dwFileSubtype As Long          '  e.g. VFT2_DRV_KEYBOARD
        dwFileDateMS As Long           '  e.g. 0
        dwFileDateLS As Long           '  e.g. 0
End Type


Public 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

Public Declare Function GetFileVersionInfoSize Lib "Version.dll" _
         Alias "GetFileVersionInfoSizeA" _
         (ByVal lptstrFilename As String, _
         lpdwHandle As Long) As Long

Public Declare Function VerQueryValue Lib "Version.dll" _
         Alias "VerQueryValueA" _
         (pBlock As Any, _
         ByVal lpSubBlock As String, _
         lplpBuffer As Any, _
         puLen As Long) As Long

Public Declare Function GetSystemDirectory Lib "kernel32" _
         Alias "GetSystemDirectoryA" _
         (ByVal Path As String, _
         ByVal cbBytes As Long) As Long

Public Declare Sub MoveMemory Lib "kernel32" _
         Alias "RtlMoveMemory" _
         (dest As Any, _
         ByVal Source As Long, _
         ByVal Length As Long)

Public Declare Function lstrcpy Lib "kernel32" _
         Alias "lstrcpyA" _
         (ByVal lpString1 As String, _
         ByVal lpString2 As Long) As Long


Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_TOP = 0
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40
Public Const HWND_TOPMOST = -1

Public Declare Function LockFile Lib "kernel32" (ByVal hFile As Long, ByVal dwFileOffsetLow As Long, ByVal dwFileOffsetHigh As Long, ByVal nNumberOfBytesToLockLow As Long, ByVal nNumberOfBytesToLockHigh As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1

Public Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long

Function FileVersion(file As String) As Long
    Dim ver() As Byte, size As Long, bytebuffer(255) As Byte, pBuff As Long
    Dim Lang_Charset_String As String, HexNumber As Long, strTemp As String
    Dim sVersion As String, v As Long, f1 As Integer, f2 As Integer
    
    On Error Resume Next
    size = GetFileVersionInfoSize(file, 0)
    
    If size > 0 Then
        ReDim ver(size) ' alloc memory
    
        If GetFileVersionInfo(file, 0, size, ver(0)) Then
            VerQueryValue ver(0), "\VarFileInfo\Translation", pBuff, size
    
            MoveMemory bytebuffer(0), pBuff, size
            
            
            HexNumber = bytebuffer(2) + bytebuffer(3) * &H100 + _
                        bytebuffer(0) * &H10000 + bytebuffer(1) * &H1000000
            Lang_Charset_String = Hex(HexNumber)
            '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(Lang_Charset_String) < 8
                Lang_Charset_String = "0" & Lang_Charset_String
            Loop

            strTemp = "\StringFileInfo\" & Lang_Charset_String _
                      & "\" & "ProductVersion"

            VerQueryValue ver(0), strTemp, pBuff, size
            
            sVersion = String(size + 1, 0)
            
            lstrcpy sVersion, pBuff
            
            sVersion = Mid$(sVersion, 1, InStr(sVersion, Chr(0)) - 1)
            
            f2 = 1
            f1 = 1
            Do While f1 > 0
                f1 = InStr(f2, sVersion, ".")
                If f1 > f2 Then
                    v = v * 10000 + Val(Mid(sVersion, f2, f1 - f2))
                    f2 = f1 + 1
                End If
            Loop
            v = v * 10000 + Val(Mid(sVersion, f2))
            
            FileVersion = v

        End If
        
        ReDim ver(0)    ' free memory
    End If
    
End Function





