VB:Tutorials:WINAPI:Using INI files
From GDWiki
The INI file is a bit of a relic, as the function of storing program settings is now relegated to the Windows Registry. While the registry is well suited for this task, if you plan to use a large number of entries, want to ensure the user can edit the values without breaking out Regedit, or just don't want to risk screwing something up with a typo, an INI file is both familiar and easy to work with.
[edit] Declarations
The INI format is pretty basic...
[header] key=data
The Windows API includes function calls that will do all of the work for you.
Public Declare Function GetPrivateProfileStringA Lib "kernel32.dll" (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
Public Declare Function WritePrivateProfileStringA Lib "kernel32.dll" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Both:
lpApplicationName is the section header.
lpKeyName is the key's name.
lpFileName is the filename of the INI.
Get:
lpDefault is what you want the function to return if the key doesn't exist.
lpReturnedString is the String variable that will receive the data from the INI.
nSize is the width of the passed string.
Write:
lpString is the string to store.
[edit] Reading and Writing a INI Key
Reading a key is pretty simple, just send it a hearty string for it to fill. The call returns a Long that gives the width of the key's data. The Left() function cuts off the irrelevant part of the returned string. The width of 255 should be plenty large for most applications.
sReturn = Space(255) sReturn = Left(sReturn, GetPrivateProfileStringA(header, key, defaultReturn, sReturn, 255, filePath))
Writing a key is even easier...just supply the header, key, data, and filename.
WritePrivateProfileStringA header, key, Datum, filePath

