' Win32API Example - GetPrivateProfileString, WritePrivateProfileString
'
' Reading and writing to .ini files using GetProfileString, WriteProfileString,
' GetPrivateProfileString, WritePrivateProfileString
'
' Jon Vote
'
' 02/2002
'
' 1) Create a new project. Form1 will be created by default.
' 2) Stack four Label, Textbox pairs such that each Label has a Textbox to it's right.
' 3) Add two Command Buttons below the Label, Textbox pairs.
' 4) Add a module to the project.
' 5) Paste the following code into Form1.
' --- Begin code for Form1
' Win32API Example - GetPrivateProfileString, WritePrivateProfileString
'
' Reading and writing to .ini files using GetProfileString, WriteProfileString,
' GetPrivateProfileString, WritePrivateProfileString
'
' Jon Vote
'
' 02/2002
'
Option Explicit
Private Sub Command1_Click()
Dim strIniFile As String
Dim strSection As String
Dim strKey As String
Dim strEntry As String
strIniFile = Trim$(Text1.Text)
strSection = Trim$(Text2.Text)
strKey = Trim$(Text3.Text)
strEntry = Trim$(Text4.Text)
Call PutIni(strIniFile, strSection, strKey, strEntry)
End Sub
Private Sub Command2_Click()
Text4.Text = GetIni(Text1.Text, Text2.Text, Text3.Text)
End Sub
Private Sub Form_Load()
Me.Caption = "Win32 API Example - .ini files."
Label1.Caption = "Ini File:"
Label2.Caption = "Section:"
Label3.Caption = "Key:"
Label4.Caption = "Entry:"
Text1.Text = App.Path & "\" & App.EXEName & ".ini"
Text2.Text = "TestSection"
Text3.Text = "TestKey"
Text4.Text = "TestEntry"
Command1.Caption = "PutIni"
Command2.Caption = "GetIni"
End Sub
' --- End code for Form1 ---
' 6) Paste the following code into Module1.
' --- Begin code for Module1 ---
'GetIni, PutIni - Reading and Write to .ini files
'Jon Vote
'
'02/2002
Option Explicit
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
'Returns entry from .ini file:
'
'(strIniFile)
'
'[Section]
'Key=Entry
'
Function GetIni(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String) As String
Dim lngRC As Long
Dim strEntry As String
strEntry = Space$(256)
lngRC = GetPrivateProfileString(strSection, strKey, "", strEntry, Len(strEntry), strIniFile)
strEntry = Left$(strEntry, lngRC)
GetIni = strEntry
End Function
'Puts an entry to an .ini file:
'
'(strIniFile)
'
'[Section]
'Key=Entry
'
Sub PutIni(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String, ByVal strEntry As String)
' puts an entry to the .ini file
Dim lngRC As Long
lngRC = WritePrivateProfileString(strSection, strKey, strEntry, strIniFile)
End Sub
' --- End code for Module1 ---