home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Module1"
- Option Explicit
-
- #If Win16 Then
- Declare Function DiskSpaceFree Lib "SETUPKIT.DLL" () As Long
- Declare Sub DLLSelfRegister Lib "SETUPKIT.DLL" _
- (ByVal lpDllName As String)
- Declare Function GetPrivateProfileString Lib _
- "Kernel" (ByVal lpApplicationName As String, _
- ByVal lpKeyName As Any, ByVal lpDefault As String, _
- ByVal lpReturnedString As String, ByVal nSize As Integer, _
- ByVal lpFileName As String) As Integer
- Declare Function WritePrivateProfileString Lib "Kernel" _
- (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, _
- ByVal lpString As Any, ByVal lplFileName As String) _
- As Integer
- Declare Function GetWindowsDirectory Lib "Kernel" _
- (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
- Declare Function GetSystemDirectory Lib "Kernel" _
- (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
- Declare Function GetDriveType16 Lib "Kernel" Alias _
- "GetDriveType" (ByVal intDriveNum As Integer) As Integer
- Declare Function GetTempFileName16 Lib "Kernel" Alias _
- "GetTempFileName" (ByVal cDriveLetter As Integer, _
- ByVal lpPrefixString As String, ByVal wUnique As Integer, _
- ByVal lpTempFileName As String) As Integer
- Declare Function GetFileVersionInfoSize Lib "VER.DLL" _
- (ByVal strFileName As String, lVerHandle As Long) As Long
- Declare Function GetFileVersionInfo Lib "VER.DLL" _
- (ByVal strFileName As String, ByVal lVerHandle As Long, _
- ByVal lcbSize As Long, ByVal strDATA As String) As Integer
- Declare Function VerQueryValue Lib "VER.DLL" _
- (ByVal strVerData As String, ByVal lpszSubBlock As String, _
- lplpBuf As Long, lpcb As Long) As Integer
- Declare Function VerInstallFile Lib "VER.DLL" _
- (ByVal Flags As Integer, _
- ByVal SrcName As String, _
- ByVal DestName As String, _
- ByVal SrcDir As String, _
- ByVal DestDir As String, _
- ByVal CurrDir As Any, _
- ByVal TmpName As String, _
- iTempLen As Integer) As Long
- Declare Function VerFindFile Lib "VER.DLL" _
- (ByVal iFlags As Integer, _
- ByVal strFileName As String, _
- ByVal strWinDirectory As String, _
- ByVal strAppDir As String, _
- ByVal strCurDir As String, _
- iCurDirLen As Integer, _
- ByVal strDestDir As String, _
- iDestDirLen As Integer) _
- As Integer
- #Else
- Declare Function DiskSpaceFree Lib "STKIT432.DLL" _
- Alias "DISKSPACEFREE" () As Long
- Declare Function GetWinPlatform Lib "STKIT432.DLL" () As Long
- Declare Function FSyncShell Lib "STKIT432.DLL" _
- Alias "SyncShell" (ByVal strCmdLine As String, _
- ByVal intCmdShow As Long) As Long
- Declare Sub DLLSelfRegister Lib "STKIT432.DLL" _
- (ByVal lpDllName As String)
- Declare Function GetPrivateProfileString Lib "Kernel32" _
- Alias "GetPrivateProfileStringA" _
- (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
- ByVal lpDefault As String, ByVal lpReturnedString As String, _
- ByVal lSize As Long, ByVal lpFileName As String) As Long
- Declare Function WritePrivateProfileString Lib "Kernel32" Alias _
- "WritePrivateProfileStringA" (ByVal lpApplicationName As Any, _
- ByVal lpKeyName As Any, ByVal lpString As Any, _
- ByVal lplFileName As String) As Long
- Declare Function GetWindowsDirectory Lib "Kernel32" _
- Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
- ByVal nSize As Long) As Long
- Declare Function GetSystemDirectory Lib "Kernel32" Alias _
- "GetSystemDirectoryA" (ByVal lpBuffer As String, _
- ByVal nSize As Long) As Long
- Declare Function GetDriveType32 Lib "Kernel32" Alias _
- "GetDriveTypeA" (ByVal strWhichDrive As String) As Long
- Declare Function GetTempFileName32 Lib "Kernel32" Alias _
- "GetTempFileNameA" (ByVal strWhichDrive As String, _
- ByVal lpPrefixString As String, ByVal wUnique As Integer, _
- ByVal lpTempFileName As String) As Long
- Declare Function VerInstallFile Lib "version.dll" _
- Alias "VerInstallFileA" (ByVal uFlags As Long, _
- ByVal szSrcFileName As String, _
- ByVal szDestFileName As String, _
- ByVal szSrcDir As String, _
- ByVal szDestDir As String, _
- ByVal szCurDir As String, _
- ByVal szTmpFile As String, _
- lpuTmpFileLen As Long) As Long
- Declare Function GetFileVersionInfoSize Lib "version.dll" _
- Alias "GetFileVersionInfoSizeA" (ByVal strFileName As String, _
- lVerHandle As Long) As Long
- Declare Function GetFileVersionInfo Lib "version.dll" _
- Alias "GetFileVersionInfoA" (ByVal strFileName As String, _
- ByVal lVerHandle As Long, ByVal lcbSize As Long, _
- lpvData As Byte) As Long
- Declare Function VerQueryValue Lib "version.dll" _
- Alias "VerQueryValueA" (lpvVerData As Byte, _
- ByVal lpszSubBlock As String, lplpBuf As Long, _
- lpcb As Long) As Long
- Declare Function VerFindFile Lib "version.dll" _
- Alias "VerFindFileA" (ByVal uFlags As Long, _
- ByVal szFileName As String, _
- ByVal szWinDir As String, _
- ByVal szAppDir As String, _
- ByVal szCurDir As String, _
- lpuCurDirLen As Long, _
- ByVal szDestDir As String, _
- lpuDestDirLen As Long) As Long
-
- #End If
-
- ' VerFind flag (only one).
- Const VFFF_ISSHAREDFILE = 1
- ' VerFindFile error return codes.
- Const VFF_CURNEDEST = 1
- Const VFF_FILEINUSE = 2
- Const VFF_BUFFTOOSMALL = 4
-
- ' VerInstallFile flags.
- Const VIFF_FORCEINSTALL% = &H1
- Const VIF_TEMPFILE& = &H1
- ' VerInstallFile error return codes.
- Const VIF_SRCOLD& = &H4
- Const VIF_DIFFLANG& = &H8
- Const VIF_DIFFCODEPG& = &H10
- Const VIF_DIFFTYPE& = &H20
- Const VIF_WRITEPROT& = &H40
- Const VIF_FILEINUSE& = &H80
- Const VIF_OUTOFSPACE& = &H100
- Const VIF_ACCESSVIOLATION& = &H200
- Const VIF_SHARINGVIOLATION = &H400
- Const VIF_CANNOTCREATE = &H800
- Const VIF_CANNOTDELETE = &H1000
- Const VIF_CANNOTRENAME = &H2000
- Const VIF_OUTOFMEMORY = &H8000
- Const VIF_CANNOTREADSRC = &H10000
- Const VIF_CANNOTREADDST = &H20000
- Const VIF_BUFFTOOSMALL = &H40000
-
- Sub InstallFile(strSrcFile, strSrcDir, strDestFile, strDestDir)
- Dim strWinDir As String, strCurDir As String, _
- strAppDir As String, strTmpFile As String
- #If Win16 Then
- Dim iWorked As Integer, iLen As Integer
- #Else
- Dim iWorked As Long, iLen As Long
- #End If
- Dim lWorked As Long
- strSrcDir = "b:\"
- strWinDir = GetWinDir
- strAppDir = "c:\olestore"
- strCurDir = CurDir$
- strDestDir = Space(255)
- iLen = Len(strDestDir)
- iWorked = VerFindFile(VFFF_ISSHAREDFILE, strDestFile, strWinDir, _
- strAppDir, strCurDir, Len(strCurDir), _
- strDestDir, iLen)
- Select Case iWorked
- ' File not found, so OK to install.
- Case VFF_CURNEDEST
- ' Install file (0& indicates no pre-existing file)
- lWorked = VerInstallFile(0, _
- strSrcFile, _
- strDestFile, _
- strSrcDir, _
- strDestDir, _
- 0&, _
- strTmpFile, _
- iLen)
- ' File is locked and can't be overwritten.
- Case VFF_FILEINUSE
- GoTo errInstallFile
- ' Destination directory string not big enough.
- Case VFF_BUFFTOOSMALL
- GoTo errInstallFile
- ' File was found, so compare versions.
- Case Else
- If iLen Then
- strTmpFile = Space(255)
- iLen = Len(strTmpFile)
- lWorked = VerInstallFile(0, _
- strSrcFile, _
- strDestFile, _
- strSrcDir, _
- strDestDir, _
- strDestDir, _
- strTmpFile, _
- iLen)
- If lWorked And VIF_SRCOLD Then
- 'Source file was older, not copied
- ElseIf lWorked And (VIF_DIFFLANG Or VIF_DIFFCODEPG Or VIF_DIFFTYPE) Then
- ' Retry and force installation.
- ' May want to prompt here in your code...
- lWorked = VerInstallFile(VIFF_FORCEINSTALL, _
- strSrcFile, _
- strDestFile, _
- strSrcDir, _
- strDestDir, _
- strDestDir, _
- strTmpFile, _
- iLen)
- Else
- GoTo errInstallFile
- End If
- End If
- End Select
- Exit Sub
- errInstallFile:
- ' Error handler for installation errors.
- ' VerFindFile errors.
- If iWorked = VIF_FILEINUSE Then
- ' Notify user.
- Else
- ' Stop program.
- End If
- ' VerInstallFile errors.
- If lWorked And VIF_WRITEPROT Then
- ElseIf lWorked And VIF_FILEINUSE Then
- ElseIf lWorked And VIF_OUTOFSPACE Then
- ElseIf lWorked And VIF_ACCESSVIOLATION Then
- ElseIf lWorked And VIF_SHARINGVIOLATION Then
- ElseIf lWorked And VIF_OUTOFMEMORY Then
- Else
- ' For these cases, report the error and do not install the file
- If lWorked And VIF_CANNOTCREATE Then
- ElseIf lWorked And VIF_CANNOTDELETE Then
- ElseIf lWorked And VIF_CANNOTRENAME Then
- ElseIf lWorked And VIF_CANNOTREADSRC Then
- ElseIf lWorked And VIF_CANNOTREADDST Then
- ElseIf lWorked And VIF_BUFFTOOSMALL Then
- End If
- End If
- End Sub
- ' Returns the Windows directory.
- Function GetWinDir() As String
- Dim strWinDirectory As String
- Dim iWorked As Integer
- ' Allocate space for the returned path string.
- strWinDirectory = Space(144)
- ' Get the Windows directory.
- iWorked = GetWindowsDirectory(strWinDirectory, Len(strWinDirectory))
- ' Trim off the excess space.
- GetWinDir = Left(strWinDirectory, iWorked)
- End Function
- Sub main()
- ' Demonstrate GetWinDir.
- MsgBox "Windows directory: " & GetWinDir
- ' Demonstrate InstallFile (need distribution disk).
- ' InstallFile "olestore.ex_", "b:\", "olestore.exe", "c:\olestore"
- End Sub
-
-
-