home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / cusgen12 / setup1.bas < prev    next >
BASIC Source File  |  1993-02-19  |  19KB  |  577 lines

  1.  
  2. '-------------------------------------------------------
  3. 'Centers the passed form just above center on the screen
  4. '-------------------------------------------------------
  5. Sub CenterForm (x As Form)
  6.   
  7.     Screen.MousePointer = 11
  8.     x.Top = (Screen.Height * .85) / 2 - x.Height / 2
  9.     x.Left = Screen.Width / 2 - x.Width / 2
  10.     Screen.MousePointer = 0
  11.  
  12. End Sub
  13.  
  14. '---------------------------------------------------------------
  15. 'Copies file Filename from SourcePath to DestinationPath.
  16. 'If VerFlag is set to true (-1) then use version checking
  17. 'algorithm so older versions are not copied over newer versions
  18. '
  19. 'Returns 0 if it could not find the file, or other runtime
  20. 'error occurs.  Otherwise, returns true.
  21. '
  22. 'If the source file is older, and the older% parameter is
  23. 'true, the function returns success (-1) even though no
  24. 'file was copied, since no error occurred.
  25. '---------------------------------------------------------------
  26. Function CopyFile (ByVal SourcePath As String, ByVal DestinationPath As String, ByVal filename As String, VerFlag As Integer)
  27.     Dim Index As Integer
  28.     Dim FileLength As Long
  29.     Dim LeftOver As Long
  30.     Dim FileData As String
  31.  
  32.     Screen.MousePointer = 11
  33.  
  34.     '--------------------------------------
  35.     'Add ending \ symbols to path variables
  36.     '--------------------------------------
  37.     If Right$(SourcePath$, 1) <> "\" Then
  38.     SourcePath$ = SourcePath$ + "\"
  39.     End If
  40.     If Right$(DestinationPath$, 1) <> "\" Then
  41.     DestinationPath$ = DestinationPath$ + "\"
  42.     End If
  43.     
  44.     '----------------------------
  45.     'Update status dialog info
  46.     '----------------------------
  47.     Statusdlg.Label1.Caption = "Source file: " + Chr$(10) + Chr$(13) + UCase$(SourcePath$ + filename$)
  48.     Statusdlg.Label1.Refresh
  49.     Statusdlg.Label2.Caption = "Destination file: " + Chr$(10) + Chr$(13) + UCase$(DestinationPath$ + filename$)
  50.     Statusdlg.Label2.Refresh
  51.  
  52.     If Not FileExists(SourcePath$ + filename$) Then
  53.     MsgBox "Error occurred while attempting to copy file.  Could not locate file: """ + SourcePath$ + filename$ + """", 64, "SETUP"
  54.     GoTo ErrorCopy
  55.     End If
  56.     
  57.     On Error GoTo ErrorCopy
  58.  
  59.     '-------------------------------------------------
  60.     ' If version checking set to True, then get their
  61.     ' version info, skip if older version
  62.     '-------------------------------------------------
  63.     If VerFlag Then
  64.     szBufSrc$ = String$(255, 32)
  65.     Call GetFileVersion(SourcePath$ + filename$, szBufSrc$, Len(szBufSrc$))
  66.     
  67.     szBufDest$ = String$(255, 32)
  68.     Call GetFileVersion(DestinationPath$ + filename$, szBufDest$, Len(szBufDest$))
  69.     
  70.     If szBufSrc$ < szBufDest$ Then GoTo SkipCopy
  71.     
  72.     End If
  73.     
  74.  
  75.     '-------------
  76.     'Copy the file
  77.     '-------------
  78.     'Const BlockSize = 32768
  79.      Const blocksize = 15322
  80.     Open SourcePath$ + filename$ For Binary Access Read As #1
  81.  
  82.     Open DestinationPath$ + filename$ For Output As #2
  83.     Close #2
  84.  
  85.     Open DestinationPath$ + filename$ For Binary As #2
  86.     
  87.     FileLength = LOF(1)
  88.     UpdateStatus FileLength
  89.     
  90.     NumBlocks = FileLength \ blocksize
  91.     LeftOver = FileLength Mod blocksize
  92.     
  93.     FileData = String$(LeftOver, 32)
  94.     
  95.     Get #1, , FileData
  96.     Put #2, , FileData
  97.     
  98.     FileData = String$(blocksize, 32)
  99.     
  100.     For Index = 1 To NumBlocks
  101.     Get #1, , FileData
  102.     Put #2, , FileData
  103.     Next Index
  104.     
  105.     Close #1, #2
  106.     x = SetFileDateTime(SourcePath$ + filename$, DestinationPath$ + filename$)
  107.  
  108. SkipCopy:
  109.     szBufSrc$ = ""
  110.     szBufDest$ = ""
  111.     Screen.MousePointer = 0
  112.     CopyFile = True
  113.     Exit Function
  114.  
  115.  
  116. ErrorCopy:
  117.     CopyFile = False
  118.     Close
  119.     Resume
  120. End Function
  121.  
  122. '---------------------------------------------
  123. 'Create the path contained in DestPath$
  124. 'First char must be drive letter, followed by
  125. 'a ":\" followed by the path, if any.
  126. '---------------------------------------------
  127. Function CreatePath (ByVal destpath$) As Integer
  128.     Screen.MousePointer = 11
  129.  
  130.     '---------------------------------------------
  131.     'Add slash to end of path if not there already
  132.     '---------------------------------------------
  133.     If Right$(destpath$, 1) <> "\" Then
  134.     destpath$ = destpath$ + "\"
  135.     End If
  136.       
  137.  
  138.     '-----------------------------------
  139.     'Change to the root dir of the drive
  140.     '-----------------------------------
  141.     On Error Resume Next
  142.     ChDrive destpath$
  143.     If Err <> 0 Then GoTo errorOut
  144.     ChDir "\"
  145.  
  146.     '-------------------------------------------------
  147.     'Attempt to make each directory, then change to it
  148.     '-------------------------------------------------
  149.     BackPos = 3
  150.     forePos = InStr(4, destpath$, "\")
  151.     Do While forePos <> 0
  152.     temp$ = Mid$(destpath$, BackPos + 1, forePos - BackPos - 1)
  153.  
  154.     Err = 0
  155.     MkDir temp$
  156.     If Err <> 0 And Err <> 75 Then GoTo errorOut
  157.  
  158.     Err = 0
  159.     ChDir temp$
  160.     If Err <> 0 Then GoTo errorOut
  161.  
  162.     BackPos = forePos
  163.     forePos = InStr(BackPos + 1, destpath$, "\")
  164.     Loop
  165.          
  166.     CreatePath = True
  167.     Screen.MousePointer = 0
  168.     Exit Function
  169.          
  170. errorOut:
  171.     MsgBox "Error While Attempting to Create Directories on Destination Drive.", 48, "SETUP"
  172.     CreatePath = False
  173.     Screen.MousePointer = 0
  174.  
  175. End Function
  176.  
  177. '-------------------------------------------------------------
  178. ' Procedure: CreateProgManGroup
  179. ' Arguments: X           The Form where a Label1 exist
  180. '            GroupName$  A string that contains the group name
  181. '            GroupPath$  A string that contains the group file
  182. '                        name  ie 'myapp.grp'
  183. '-------------------------------------------------------------
  184. Sub CreateProgManGroup (x As Form, GroupName$, GroupPath$)
  185.     
  186.     Screen.MousePointer = 11
  187.     
  188.     '----------------------------------------------------------------------
  189.     'Windows requires DDE in order to create a program group and item.
  190.     'Here, a Visual Basic label control is used to generate the DDE messages
  191.     '----------------------------------------------------------------------
  192.     On Error Resume Next
  193.  
  194.     
  195.     '--------------------------------
  196.     'Set LinkTopic to PROGRAM MANAGER
  197.     '--------------------------------
  198.     x.Label1.LinkTopic = "ProgMan|Progman"
  199.     x.Label1.LinkMode = 2
  200.     For i% = 1 To 10                                         ' Loop to ensure that there is enough time to
  201.       z% = DoEvents()                                        ' process DDE Execute.  This is redundant but needed
  202.     Next                                                     ' for debug windows.
  203.     x.Label1.LinkTimeout = 100
  204.  
  205.  
  206.     '---------------------
  207.     ' Create program group
  208.     '---------------------
  209.     x.Label1.LinkExecute "[CreateGroup(" + GroupName$ + Chr$(44) + GroupPath$ + ")]"
  210.  
  211.  
  212.     '-----------------
  213.     'Reset properties
  214.     '-----------------
  215.     x.Label1.LinkTimeout = 50
  216.     x.Label1.LinkMode = 0
  217.     
  218.     Screen.MousePointer = 0
  219. End Sub
  220.  
  221. '----------------------------------------------------------
  222. ' Procedure: CreateProgManItem
  223. '
  224. ' Arguments: X           The form where Label1 exists
  225. '
  226. '            CmdLine$    A string that contains the command
  227. '                        line for the item/icon.
  228. '                        ie 'c:\myapp\setup.exe'
  229. '
  230. '            IconTitle$  A string that contains the item's
  231. '                        caption
  232. '----------------------------------------------------------
  233. Sub CreateProgManItem (x As Form, CmdLine$, IconTitle$)
  234.     
  235.     Screen.MousePointer = 11
  236.     
  237.     '----------------------------------------------------------------------
  238.     'Windows requires DDE in order to create a program group and item.
  239.     'Here, a Visual Basic label control is used to generate the DDE messages
  240.     '----------------------------------------------------------------------
  241.     On Error Resume Next
  242.  
  243.  
  244.     '---------------------------------
  245.     'Set LinkTopic to PROGRAM MANAGER
  246.     '---------------------------------
  247.     x.Label1.LinkTopic = "ProgMan|Progman"
  248.     x.Label1.LinkMode = 2
  249.     For i% = 1 To 10                                         ' Loop to ensure that there is enough time to
  250.       z% = DoEvents()                                        ' process DDE Execute.  This is redundant but needed
  251.     Next                                                     ' for debug windows.
  252.     x.Label1.LinkTimeout = 100
  253.  
  254.     
  255.     '------------------------------------------------
  256.     'Create Program Item, one of the icons to launch
  257.     'an application from Program Manager
  258.     '------------------------------------------------
  259.     x.Label1.LinkExecute "[AddItem(" + CmdLine$ + Chr$(44) + IconTitle$ + Chr$(44) + ",,)]"
  260.     
  261.     '-----------------
  262.     ' Reset properties
  263.     '-----------------
  264.     x.Label1.LinkTimeout = 50
  265.     x.Label1.LinkMode = 0
  266.     
  267.     Screen.MousePointer = 0
  268. End Sub
  269.  
  270. '----------------------------------------------------------
  271. ' Check for the existence of a file by attempting an OPEN.
  272. '----------------------------------------------------------
  273. Function FileExists (path$) As Integer
  274.  
  275.     x = FreeFile
  276.  
  277.     On Error Resume Next
  278.     Open path$ For Input As x
  279.     If Err = 0 Then
  280.     FileExists = True
  281.     Else
  282.     FileExists = False
  283.     End If
  284.     Close x
  285.  
  286. End Function
  287.  
  288. '------------------------------------------------
  289. 'Get the disk space free for the current drive
  290. '------------------------------------------------
  291. Function GetDiskSpaceFree (drive As String) As Long
  292.     ChDrive drive
  293.     GetDiskSpaceFree = DiskSpaceFree()
  294. End Function
  295.  
  296. '----------------------------------------------------
  297. '  Get the disk Allocation unit for the current drive
  298. '----------------------------------------------------
  299. Function GetDrivesAllocUnit (drive As String) As Long
  300.     ChDrive drive
  301.     GetDrivesAllocUnit = AllocUnit()
  302. End Function
  303.  
  304. '------------------------
  305. 'Get the size of the file
  306. '------------------------
  307. Function GetFileSize (Source$) As Long
  308.     x = FreeFile
  309.     Open Source$ For Binary Access Read As x
  310.     GetFileSize = LOF(x)
  311.     Close x
  312. End Function
  313.  
  314. '--------------------------------------------------
  315. 'Calls the windows API to get the windows directory
  316. '--------------------------------------------------
  317. Function GetWindowsDir () As String
  318.     temp$ = String$(145, 0)              'Size Buffer
  319.     x = GetWindowsDirectory(temp$, 145)  'Make API Call
  320.     temp$ = Left$(temp$, x)              'Trim Buffer
  321.  
  322.     If Right$(temp$, 1) <> "\" Then      'Add \ if necessary
  323.     GetWindowsDir$ = temp$ + "\"
  324.     Else
  325.     GetWindowsDir$ = temp$
  326.     End If
  327. End Function
  328.  
  329. '---------------------------------------------------------
  330. 'Calls the windows API to get the windows\SYSTEM directory
  331. '---------------------------------------------------------
  332. Function GetWindowsSysDir () As String
  333.     temp$ = String$(145, 0)                 'Size Buffer
  334.     x = GetSystemDirectory(temp$, 145)      'Make API Call
  335.     temp$ = Left$(temp$, x)                 'Trim Buffer
  336.  
  337.     If Right$(temp$, 1) <> "\" Then         'Add \ if necessary
  338.     GetWindowsSysDir$ = temp$ + "\"
  339.     Else
  340.     GetWindowsSysDir$ = temp$
  341.     End If
  342. End Function
  343.  
  344. '------------------------------------------------------
  345. ' Function:   IsValidPath as integer
  346. ' arguments:  DestPath$         a string that is a full path
  347. '             DefaultDrive$     the default drive.  eg.  "C:"
  348. '
  349. '  If DestPath$ does not include a drive specification,
  350. '  IsValidPath uses Default Drive
  351. '
  352. '  When IsValidPath is finished, DestPath$ is reformated
  353. '  to the format "X:\dir\dir\dir\"
  354. '
  355. ' Result:  True (-1) if path is valid.
  356. '          False (0) if path is invalid
  357. '-------------------------------------------------------
  358. Function IsValidPath (destpath$, ByVal DefaultDrive$) As Integer
  359.  
  360.     '----------------------------
  361.     'Remove left and right spaces
  362.     '----------------------------
  363.     destpath$ = RTrim$(LTrim$(destpath$))
  364.     
  365.  
  366.     '-----------------------------
  367.     'Check Default Drive Parameter
  368.     '-----------------------------
  369.     If Right$(DefaultDrive$, 1) <> ":" Or Len(DefaultDrive$) <> 2 Then
  370.     MsgBox "Bad default drive parameter specified in IsValidPath Function.  You passed,  """ + DefaultDrive$ + """.  Must be one drive letter and "":"".  For example, ""C:"", ""D:""...", 64, "Setup Kit Error"
  371.     GoTo parseErr
  372.     End If
  373.     
  374.  
  375.     '-------------------------------------------------------
  376.     'Insert default drive if path begins with root backslash
  377.     '-------------------------------------------------------
  378.     If Left$(destpath$, 1) = "\" Then
  379.     destpath$ = DefaultDrive + destpath$
  380.     End If
  381.     
  382.     '-----------------------------
  383.     ' check for invalid characters
  384.     '-----------------------------
  385.     On Error Resume Next
  386.     tmp$ = Dir$(destpath$)
  387.     If Err <> 0 Then
  388.     GoTo parseErr
  389.     End If
  390.     
  391.  
  392.     '-----------------------------------------
  393.     ' Check for wildcard characters and spaces
  394.     '-----------------------------------------
  395.     If (InStr(destpath$, "*") <> 0) GoTo parseErr
  396.     If (InStr(destpath$, "?") <> 0) GoTo parseErr
  397.     If (InStr(destpath$, " ") <> 0) GoTo parseErr
  398.      
  399.     
  400.     '------------------------------------------
  401.     'Make Sure colon is in second char position
  402.     '------------------------------------------
  403.     If Mid$(destpath$, 2, 1) <> Chr$(58) Then GoTo parseErr
  404.     
  405.  
  406.     '-------------------------------
  407.     'Insert root backslash if needed
  408.     '-------------------------------
  409.     If Len(destpath$) > 2 Then
  410.       If Right$(Left$(destpath$, 3), 1) <> "\" Then
  411.     destpath$ = Left$(destpath$, 2) + "\" + Right$(destpath$, Len(destpath$) - 2)
  412.       End If
  413.     End If
  414.  
  415.     '-------------------------
  416.     'Check drive to install on
  417.     '-------------------------
  418.     drive$ = Left$(destpath$, 1)
  419.     ChDrive (drive$)                                                        ' Try to change to the dest drive
  420.     If Err <> 0 Then GoTo parseErr
  421.     
  422.     '-----------
  423.     'Add final \
  424.     '-----------
  425.     If Right$(destpath$, 1) <> "\" Then
  426.     destpath$ = destpath$ + "\"
  427.     End If
  428.     
  429.  
  430.     '-------------------------------------
  431.     'Root dir is a valid dir
  432.     '-------------------------------------
  433.     If Len(destpath$) = 3 Then
  434.     If Right$(destpath$, 2) = ":\" Then
  435.         GoTo ParseOK
  436.     End If
  437.     End If
  438.     
  439.  
  440.     '------------------------
  441.     'Check for repeated Slash
  442.     '------------------------
  443.     If InStr(destpath$, "\\") <> 0 Then GoTo parseErr
  444.     
  445.     '--------------------------------------
  446.     'Check for illegal directory names
  447.     '--------------------------------------
  448.     legalChar$ = "!#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`{}~."
  449.     BackPos = 3
  450.     forePos = InStr(4, destpath$, "\")
  451.     Do
  452.     temp$ = Mid$(destpath$, BackPos + 1, forePos - BackPos - 1)
  453.     
  454.     '----------------------------
  455.     'Test for illegal characters
  456.     '----------------------------
  457.     For i = 1 To Len(temp$)
  458.         If InStr(legalChar$, UCase$(Mid$(temp$, i, 1))) = 0 Then GoTo parseErr
  459.     Next i
  460.  
  461.     '-------------------------------------------
  462.     'Check combinations of periods and lengths
  463.     '-------------------------------------------
  464.     periodPos = InStr(temp$, ".")
  465.     length = Len(temp$)
  466.     If periodPos = 0 Then
  467.         If length > 8 Then GoTo parseErr                         'Base too long
  468.     Else
  469.         If periodPos > 9 Then GoTo parseErr                      'Base too long
  470.         If length > periodPos + 3 Then GoTo parseErr             'Extension too long
  471.         If InStr(periodPos + 1, temp$, ".") <> 0 Then GoTo parseErr'Two periods not allowed
  472.     End If
  473.  
  474.     BackPos = forePos
  475.     forePos = InStr(BackPos + 1, destpath$, "\")
  476.     Loop Until forePos = 0
  477.  
  478. ParseOK:
  479.     IsValidPath = True
  480.     Exit Function
  481.  
  482. parseErr:
  483.     IsValidPath = False
  484. End Function
  485.  
  486. '----------------------------------------------------
  487. ' Prompt for the next disk.  Use the FileToLookFor$
  488. ' argument to verify that the proper disk, disk number
  489. ' wDiskNum, was inserted.
  490. '----------------------------------------------------
  491. Function PromptForNextDisk (wDiskNum As Integer, FileToLookFor$) As Integer
  492.  
  493.     '-------------------------
  494.     'Test for file
  495.     '-------------------------
  496.     Ready = False
  497.     On Error Resume Next
  498.     temp$ = Dir$(FileToLookFor$)
  499.  
  500.     '------------------------
  501.     'If not found, start loop
  502.     '------------------------
  503.     If Err <> 0 Or Len(temp$) = 0 Then
  504.     While Not Ready
  505.         '----------------------------
  506.         'Put up msg box
  507.         '----------------------------
  508.         Beep
  509.         x = MsgBox("Please insert disk # " + Format$(wDiskNum%), 49, "SETUP")
  510.         If x = 2 Then
  511.         '-------------------------------
  512.         'Use hit cancel, abort the copy
  513.         '-------------------------------
  514.         PromptForNextDisk = False
  515.         GoTo ExitProc
  516.         Else
  517.         '----------------------------------------
  518.         'User hits OK, try to find the file again
  519.         '----------------------------------------
  520.         temp$ = Dir$(FileToLookFor$)
  521.         If Err = 0 And Len(temp$) <> 0 Then
  522.             PromptForNextDisk = True
  523.             Ready = True
  524.         End If
  525.         End If
  526.     Wend
  527.     Else
  528.     PromptForNextDisk = True
  529.     End If
  530.  
  531.     
  532.  
  533. ExitProc:
  534.  
  535. End Function
  536.  
  537. Sub RestoreProgMan ()
  538.     AppActivate "Program Manager"   ' Activate Program Manager.
  539.     SendKeys "%{ }{Enter}", True      ' Send Restore keystrokes.
  540. End Sub
  541.  
  542. '-----------------------------------------------------------------------------
  543. 'Set the Destination File's date and time to the Source file's date and time
  544. '-----------------------------------------------------------------------------
  545. Function SetFileDateTime (SourceFile As String, DestinationFile As String) As Integer
  546.     x = SetTime(SourceFile, DestinationFile)
  547.     SetFileDateTime = -1
  548. End Function
  549.  
  550. Sub UpdateStatus (FileLength As Long)
  551. '-----------------------------------------------------------------------------
  552. 'Update the status bar using form.control Statusdlg.Picture2
  553. '-----------------------------------------------------------------------------
  554.     Static position
  555.     Dim estTotal As Long
  556.  
  557.     estTotal = Val(Statusdlg.total.Tag)
  558.     If estTotal = False Then
  559.     estTotal = 10000000
  560.     End If
  561.  
  562.     position = position + CSng((FileLength / estTotal) * 100)
  563.     If position > 100 Then
  564.     position = 100
  565.     End If
  566.     Statusdlg.Picture2.Cls
  567.     Statusdlg.Picture2.Line (0, 0)-((position * (Statusdlg.Picture2.ScaleWidth / 100)), Statusdlg.Picture2.ScaleHeight), QBColor(4), BF
  568.  
  569.     Txt$ = Format$(CLng(position)) + "%"
  570.     Statusdlg.Picture2.CurrentX = (Statusdlg.Picture2.ScaleWidth - Statusdlg.Picture2.TextWidth(Txt$)) \ 2
  571.     Statusdlg.Picture2.CurrentY = (Statusdlg.Picture2.ScaleHeight - Statusdlg.Picture2.TextHeight(Txt$)) \ 2
  572.     Statusdlg.Picture2.Print Txt$
  573.  
  574.     r = BitBlt(Statusdlg.Picture1.hDC, 0, 0, Statusdlg.Picture2.ScaleWidth, Statusdlg.Picture2.ScaleHeight, Statusdlg.Picture2.hDC, 0, 0, SRCCOPY)
  575. End Sub
  576.  
  577.