home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / various / sizing / profile.bas < prev    next >
Encoding:
BASIC Source File  |  1994-09-26  |  6.2 KB  |  144 lines

  1. Option Explicit
  2. '   This information was obtained from the MSDN CD.
  3. '
  4. '   Declare API calls needed to read/write to INI files
  5. '
  6. Declare Function KRN_GetProfileInt Lib "Kernel" Alias "GetProfileInt" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal mnDefault As Integer) As Integer
  7. Declare Function KRN_GetProfileString Lib "Kernel" Alias "GetProfileString" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal msDefault As String, ByVal msReturnString As String, ByVal msReturnSize As Integer) As Integer
  8. Declare Function KRN_WriteProfileString Lib "Kernel" Alias "WriteProfileString" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal msEntry As String) As Integer
  9. Declare Function KRN_GetPrivateProfileInt Lib "Kernel" Alias "GetPrivateProfileInt" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal mnDefault As Integer, ByVal msININame As String) As Integer
  10. Declare Function KRN_GetPrivateProfileString Lib "Kernel" Alias "GetPrivateProfileString" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal msDefault As String, ByVal msReturnString As String, ByVal msReturnSize As Integer, ByVal msININame As String) As Integer
  11. Declare Function KRN_WritePrivateProfileString Lib "Kernel" Alias "WritePrivateProfileString" (ByVal msHeader As String, ByVal msKeyWord As String, ByVal msEntry As String, ByVal msININame As String) As Integer
  12. '
  13. '   Parameters are as follows:
  14. '
  15. '   Argument           Description
  16. '   --------           -----------
  17. '   msHeader            Name of a Windows application that appears in the
  18. '                       initialization file.
  19. '   msKeyWord           Key name that appears in the initialization file.
  20. '   mnDefault           Specifies the default value for the given key if the
  21. '                       key cannot be found in the initialization file.
  22. '   msININame           Points to a string that names the initialization
  23. '                       file. If lpFileName does not contain a path to the
  24. '                       file, Windows searches for the file in the Windows
  25. '                       directory.
  26. '   msDefault           Specifies the default value for the given key if the
  27. '                       key cannot be found in the initialization file.
  28. '   msReturnString      Specifies the buffer that receives the character
  29. '                       string.
  30. '   msReturnSize        Specifies the maximum number of characters (including
  31. '                       the last null character) to be copied to the buffer.
  32. '   msEntry           Specifies the string that contains the new key value.
  33.  
  34. Function ProfileInt (sININame As String, sSection As String, sKeyword As String, nDefault As Integer) As Integer
  35. '
  36. '   This routine will get a string from an INI file. The user must pass the
  37. '   following information:
  38. '
  39. '   Input Values
  40. '       sININame   - The name of the INI file (with path if desired)
  41. '       sSection   - The section head to search for
  42. '       sKeyword   - The keyword within the section to return
  43. '       nDefault   - The default value to return if nothing is found
  44. '
  45. '   Output Value
  46. '       An integer with the value found or nDefault. NOTE: IF A RETURN VALUE
  47. '       OF ZERO IS OBTAINED, AND nDefault IS NOT ZERO, THEN THE SPECIFIED ITEM
  48. '       IS NOT AN INTEGER
  49. '
  50.     On Error GoTo ProfileInt_ER
  51.  
  52.     If IsEmpty(sININame) Or IsEmpty(sSection) Or IsEmpty(sKeyword) Then
  53.         ProfileInt = nDefault
  54.         Exit Function
  55.     End If
  56.                      
  57.     If Right$(UCase$(sININame), 7) = "WIN.INI" Then
  58.         ProfileInt = KRN_GetProfileInt(sSection, sKeyword, nDefault)
  59.     Else
  60.         ProfileInt = KRN_GetPrivateProfileInt(sSection, sKeyword, nDefault, sININame)
  61.     End If
  62.     Exit Function
  63.  
  64. ProfileInt_ER:
  65.     ProfileInt = nDefault
  66.     Exit Function
  67. End Function
  68.  
  69. Function ProfileString (sININame As String, sSection As String, sKeyword As String, vsDefault As String) As String
  70. '
  71. '   This routine will get a string from an INI file. The user must pass the
  72. '   following information:
  73. '
  74. '   Input Values
  75. '       sININame   - The name of the INI file (with path if desired)
  76. '       sSection   - The section head to search for
  77. '       sKeyword   - The keyword within the section to return
  78. '       vsDefault   - The default value to return if nothing is found
  79. '
  80. '   Output Value
  81. '       A string with the value found (max length = 512)
  82. '
  83.     Dim sReturnValue As String
  84.     Dim nReturnSize As Integer
  85.     Dim nValidSize As Integer
  86.  
  87.     On Error GoTo ProfileString_ER
  88.  
  89.     If IsEmpty(sININame) Or IsEmpty(sSection) Or IsEmpty(sKeyword) Then
  90.         ProfileString = vsDefault
  91.         Exit Function
  92.     End If
  93.  
  94.     sReturnValue = Space$(512)
  95.     nReturnSize = Len(sReturnValue)
  96.  
  97.     If Right$(UCase$(sININame), 7) = "WIN.INI" Then
  98.         nValidSize = KRN_GetProfileString(sSection, sKeyword, vsDefault, sReturnValue, nReturnSize)
  99.     Else
  100.         nValidSize = KRN_GetPrivateProfileString(sSection, sKeyword, vsDefault, sReturnValue, nReturnSize, sININame)
  101.     End If
  102.  
  103.     ProfileString = Left$(sReturnValue, nValidSize)
  104.     Exit Function
  105.  
  106. ProfileString_ER:
  107.     ProfileString = vsDefault
  108.     Exit Function
  109. End Function
  110.  
  111. Function SetProfileString (sININame As String, sSection As String, sKeyword As String, vsEntry As String) As Integer
  112. '
  113. '   This routine will write a string to an INI file. The user must pass the
  114. '   following information:
  115. '
  116. '   Input Values
  117. '       sININame   - The name of the INI file (with path if desired)
  118. '       sSection   - The section head to search for
  119. '       sKeyword   - The keyword within the section to return
  120. '       vsEntry     - The value to write for the keyword
  121. '
  122. '   Output Value
  123. '       non-zero if successful, zero if failure.
  124. '
  125.     On Error GoTo SetProfileString_ER
  126.  
  127.     If IsEmpty(sININame) Or IsEmpty(sSection) Or IsEmpty(sKeyword) Then
  128.         SetProfileString = 0
  129.         Exit Function
  130.     End If
  131.  
  132.     If Right$(UCase$(sININame), 7) = "WIN.INI" Then
  133.         SetProfileString = KRN_WriteProfileString(sSection, sKeyword, vsEntry)
  134.     Else
  135.         SetProfileString = KRN_WritePrivateProfileString(sSection, sKeyword, vsEntry, sININame)
  136.     End If
  137.     Exit Function
  138.  
  139. SetProfileString_ER:
  140.     SetProfileString = 0
  141.     Exit Function
  142. End Function
  143.  
  144.