home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap14 / libini.bas < prev    next >
BASIC Source File  |  1995-09-28  |  3KB  |  106 lines

  1. Attribute VB_Name = "libINI"
  2. Option Explicit
  3. '
  4. Global gblIniFile As String ' name of INI file/registry entry
  5.  
  6.  
  7. Function GetWinDir() As String
  8.     '
  9.     ' get the Windows launch dir
  10.     '
  11.     Dim cTemp As String
  12.     Dim nTemp As Integer
  13.     '
  14.     cTemp = String$(255, 32)    ' reserve space
  15.     nTemp = OSGetWindowsDirectory(cTemp, 255) ' call API
  16.     GetWinDir = Left$(cTemp, nTemp) ' set return value
  17. End Function
  18.  
  19. Function GetIniStr(cSection As String, ByVal cItem As String, ByVal cDefault As String)
  20.     '
  21.     ' this routine looks for cItem
  22.     ' in section cSection. If it
  23.     ' is not found, the item is
  24.     ' written using the cDefault
  25.     ' value.
  26.     '
  27.     Dim cTemp As String
  28.     '
  29.     ' try to get it
  30.     cTemp = GetSetting(gblIniFile, cSection, cItem, cDefault)
  31.     '
  32.     ' returned default? may be missing, write it to be sure
  33.     If Trim(UCase(cTemp)) = Trim(UCase(cDefault)) Then
  34.         SaveSetting gblIniFile, cSection, cItem, cDefault
  35.     End If
  36.     '
  37.     GetIniStr = cTemp   ' set return value
  38.     '
  39. End Function
  40.  
  41. 'Function GetWinDir() As String
  42. '    '
  43. '    ' get the windows launch dir
  44. '    '
  45. '    Dim cTemp As String
  46. '    Dim nTemp As Integer
  47. '    '
  48. '    cTemp = String$(255, 32)    ' reserve space
  49. '    nTemp = OSGetWindowsDirectory(cTemp, 255) ' call API
  50. '    GetWinDir = Left$(cTemp, nTemp) ' set return value
  51. 'End Function
  52.  
  53.  
  54.  
  55.  
  56. Function WriteINIStr(ByVal cSection, ByVal cItem, ByVal cDefault) As Integer
  57.     '
  58.     ' write cKeyName into cSection
  59.     SaveSetting gblIniFile, cSection, cItem, cDefault
  60.     WriteINIStr = True  ' in case they ask!
  61.     '
  62. End Function
  63.  
  64.  
  65. Public Function OpenINI(Optional cININame As Variant)
  66.     '
  67.     ' For 32-bit
  68.     '   set gblIniFile name for registry calls
  69.     '
  70.     ' For 16-bit
  71.     '   set gblIniFile name for File calls
  72.     '   if not there, create a new one
  73.     '
  74.     OpenINI = True  ' assume all goes OK
  75.     '
  76.     If IsMissing(cININame) Then ' no parm passed?
  77.         cININame = App.EXEName  ' set to this app
  78.     End If
  79.     gblIniFile = Trim(cININame) ' clean up spaces
  80.     '
  81.     Dim nFile As Integer        ' for file channel
  82.     Dim cFile As String         ' for file name
  83.     '
  84.     ' If we're running in 16-bit environment
  85.     ' we have to get to a file in the Windows
  86.     ' launch directory. We'll make sure it's
  87.     ' there and create a new one if needed.
  88.     '
  89.     #If Win16 Then
  90.         On Error Resume Next    ' keep quiet about errors
  91.         nFile = FreeFile        ' get free file channel
  92.         cFile = gblIniFile + ".INI" ' fill out file name
  93.         Open cFile For Input As nFile   ' try to open it
  94.         If Err <> 0 Then        ' some error
  95.             If Err = 53 Then    ' was it 'FileNotFound?'
  96.                 Open cFile For Output As nFile  ' create it
  97.             Else    ' uh-oh....
  98.                 MsgBox Error$, vbCritical, "Error opening INI File - " + Str(Err)
  99.                 OpenINI = False ' we failed!
  100.             End If
  101.         End If
  102.         Close nFile 'close channel
  103.     #End If
  104.     '
  105. End Function
  106.