Attribute VB_Name = "hlp_GetBuildInfo"
' Modul for Visual Basic 6
' Copyright (C) 2003-2007 Jan Vorel
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' This is a shared modul, class modul, usercontrol or form. It is also
' published under the GNU General Public License.

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

Private Type VS_FIXEDFILEINFO
  dwSignature As Long
  dwStrucVersion As Long
  dwFileVersionMS As Long
  dwFileVersionLS As Long
  dwProductVersionMS As Long
  dwProductVersionLS As Long
  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 GetFileVersionInfoA Lib "version.dll" (ByVal lptstrFilename As String, ByVal dwHandle As Long, ByVal dwLen As Long, lpData As Any) As Long
Private Declare Function GetFileVersionInfoSizeA Lib "version.dll" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
Private Declare Function VerQueryValueA Lib "version.dll" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Long, puLen As Long) As Long

Public Function GetBuildNumber(Optional ByVal ResIDDefault As Long = -1&, Optional ByVal WantFileDescInstead As Boolean = False, Optional ByVal WantFileVerInstead As Boolean) As String
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  Dim xChar2 As String

  xChar1 = AppBase()
  xLong1 = GetFileVersionInfoSizeA(xChar1, xLong2)
  If Not xLong1 = 0& Then
    xChar2 = String(xLong1, vbNullChar)
    If Not GetFileVersionInfoA(xChar1, xLong2, xLong1, ByVal xChar2) = 0& Then
      If WantFileDescInstead Then
        If Not VerQueryValueA(ByVal xChar2, "\StringFileInfo\040904B0\FileDescription" & vbNullChar, xLong1, xLong2) = 0& Then
          xChar1 = PeekSAnsi(xLong1)
          End If
        Else
        If Not VerQueryValueA(ByVal xChar2, "\StringFileInfo\040904B0\ProductName" & vbNullChar, xLong1, xLong2) = 0& Then
          xChar1 = PeekSAnsi(xLong1)
          End If
        End If
      If WantFileVerInstead Then
        If Not VerQueryValueA(ByVal xChar2, "\StringFileInfo\040904B0\FileVersion" & vbNullChar, xLong1, xLong2) = 0& Then
          GetBuildNumber = PeekSAnsi(xLong1)
          End If
        Else
        If Not VerQueryValueA(ByVal xChar2, "\StringFileInfo\040904B0\ProductVersion" & vbNullChar, xLong1, xLong2) = 0& Then
          GetBuildNumber = PeekSAnsi(xLong1)
          End If
        End If
      If Len(GetBuildNumber) = 0& Then GetBuildNumber = "???"
      If Len(xChar1) = 0& Then xChar1 = "???"
      If ResIDDefault = -1& Then
        GetBuildNumber = xChar1 & " " & GetBuildNumber
        ElseIf Not ResIDDefault = -2& Then
        GetBuildNumber = Replace(Replace(LoadResEx_String(ResIDDefault), "$V", GetBuildNumber, , , vbTextCompare), "$P", xChar1, , , vbTextCompare)
        End If
      End If
    End If
  End Function

