- Contents in this wiki are for entertainment purposes only
Vintage VB6
Jump to navigation
Jump to search
Win32 wrapper code for VB6
'' File: win32INI.bas
'' Module name: modINI
'' Author: XenoEngineer@groupKOS.com
'' Posted August 9, 2023 at https://groupKOS.com/dev
''
'' Description: This module wraps VB6 around Win32 API methods to save/retreive data to an INI file by keyword by section.
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpApplicationName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
' Loads the default variables from our private profile (INI file)
Public Function INIGetBool( _
ByVal fileSpec As String, _
ByVal Section As String, _
ByVal Key As String, _
Optional ByVal Default As String = "0" _
) As Boolean
Dim szBool As String
Dim lngLength As Long
' Load Boolean options
szBool = String$(6, Chr(0))
lngLength = GetPrivateProfileString(Section, Key, Default, szBool, 5, fileSpec)
szBool = Left$(szBool, lngLength)
Select Case True
Case Val(szBool) > 0, _
LCase(szBool) = "true", _
LCase(szBool) = "yes"
INIGetBool = True
End Select
End Function
Public Function INIGetInteger( _
ByVal fileSpec As String, _
ByVal Section As String, _
ByVal Key As String, _
Optional ByVal Default As String = "" _
) As Long
On Error Resume Next
Dim szStr As String
Dim lngLength As Long
szStr = String$(256, Chr(0))
lngLength = GetPrivateProfileString(Section, Key, Default, szStr, 255, fileSpec)
szStr = Left$(szStr, lngLength)
If Val(szStr) > 0 Then
INIGetInteger = CInt(szStr)
Else
INIGetInteger = 0
End If
End Function
Public Function INIGetString( _
ByVal fileSpec As String, _
ByVal Section As String, _
ByVal Key As String, _
Optional ByVal Default As String = "" _
) As String
Dim szStr As String
Dim lngLength As Long
szStr = String$(256, Chr(0))
' Load szStr with the INI setting
lngLength = GetPrivateProfileString(Section, Key, Default, szStr, 255, fileSpec)
szStr = Left$(szStr, lngLength)
INIGetString = szStr
End Function
Public Function INIGetSection( _
ByVal fileSpec As String, _
ByVal Section As String) As String()
' Gets the entire section of Name=Value lines as an array of
' string values of lines within the section of the INI file.
Dim strBuffer As String
On Error Resume Next
' Read the INI section into the buffer string
strBuffer = String$(2048, " ")
strBuffer = Left$(strBuffer, GetPrivateProfileSection(Section, strBuffer, Len(strBuffer), fileSpec))
strBuffer = Left$(strBuffer, Len(strBuffer) - 1) 'remove trailing null
strBuffer = Replace(strBuffer, Chr$(0), vbCr)
INIGetSection = Split(strBuffer, vbCr)
End Function
Public Sub INIDeleteSection( _
ByVal fileSpec As String, _
ByVal Key As String)
Dim Result As Long
Dim NullList As String
NullList = Chr(0) & Chr(0)
Result = WritePrivateProfileSection(Key, NullList, fileSpec)
Debug.Assert Result <> 0
End Sub
Public Function INIWrite( _
ByVal fileSpec As String, _
ByVal Section As String, _
Optional ByVal Key As String = "", _
Optional ByVal Value As String = "" _
) As Long
' Writes or deletes Key and Value
Select Case True
Case (Len(Key) > 0) And (Len(Value) > 0)
INIWrite = WritePrivateProfileString(Section, ByVal Key, ByVal Value, fileSpec)
Case (Len(Key) > 0) 'delete key's value
INIWrite = WritePrivateProfileString(Section, ByVal Key, vbNullString, fileSpec)
Case Else 'delete entire section
INIWrite = WritePrivateProfileString(Section, vbNullString, vbNullString, fileSpec)
End Select
End Function