yronium Level: Moderator

 Registered: 14-04-2002 Posts: 933
|
Managing INI files
Hello y'all. It's a long time that in the italian VB newsgroups periodically appears a thread asking how we can write and read the INI files. I always post the same solution: two little functions of mine, that belong from a code I found somewhere in andreavb.com.
My answer is always the same - and often they give me the same grateful reply - but threads expire after some time in newsgroups, so I'm afraid one of these days I can't post anymore the bare tinyurl link to the thread I spoke into, and I have to write another explanation of the whole stuff.
So I decided to post this article here: to put my functions and explanations in a not-expiring place. If one day my previous threads will be lost, I will tinyurl the link to this page (and then, my italian friends would be better start speaking english, ehehehe).
So, in order to create, read and write an INI file, copy the two routines below in a module.
Things to know:
- INI files are made of sections, which names are enclosed in square brackets into the file (you could open one by the Notepad.exe to see how it is made), and several keys for each sections. Take note that there is no space before equal sign of the key, nor after it: the format is KeyName=Value. In the routines below you have to specify the section, and the name of the key to read/edit in that section.
- When the specified INI file doesn't exist, the WriteINI routine creates a new INI file in the specified directory (if you don't specify the directory, it creates it in the executable directory. This means that, if you run it into the VB IDE by F5, it will create the INI file in the VB6.exe dir. So, you better specify it when you run it in the IDE).
- If you are working with a localized VB version, you'd be careful when you store boolean variables into the INI file: when you run the code within the IDE, VB will automatically translate into your local language the boolean values, so they will be stored in this language. For instance, my italian VB copy will automatically write the words Vero for True and Falso for False. This will cause a Type Mismatch error when later the exe will attempt to read the values, as the exe won't recognize the local values. This bug is very cranky to locate.
The solution is to get the numeric boolean value (0 or -1) before, and store the number, as following: Dim NumBoolVar As Integer
NumBoolVar = Abs(CInt(Me.AutoRedraw)) ' return 0 for False and 1 for True
Call WriteINI("FormSettings", "AutoRedrawValue", NumBoolVar, "Banner.ini") | This is not necessary when the exe will run normally, as the exe doesn't translate the VB keywords. But who writes down his code and compiles it without at least a bare IDE test run?
The last hint is a little secret (but many of you know it already): INI files must not necessarily have the .ini extension, and you can create your settings file even calling it somelike config.vxd, config.dll, config.mdb, etc. Don't use an existing filename, or you will overwrite the existing file.
Copy the following code in a module, and enjoy.
' API functions to read/write INI files
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 WritePrivateProfileString Lib _
"kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
' return the text contained in an INI file key
Public Function ReadINI(strSection As String, strKeyName As String, _
ByVal strFileName As String, Optional lStringBuffer As Long = 255, _
Optional iniDirPathName As String) As String
' WARNING: this functions uses a limited buffer, specified by
' the instruction strText = Space(buffer).
' Every exceeding char will be TRIMMED out. If you think this
' could cut off a part of the retrieved data, you can increase
' the argument in the Space() function.
Dim intLen As Long
Dim strText As String, strIniFile As String
' set INI file default directory
If iniDirPathName = "" Then iniDirPathName = App.Path
' cut off the final backslash char from path
If Right$(iniDirPathName, 1) = "" Then
Mid$(iniDirPathName, Len(iniDirPathName), 1) = Space$(1)
iniDirPathName = RTrim$(iniDirPathName)
End If
' build INI file's complete path
strIniFile = iniDirPathName & "" & strFileName
' read data in the file, and check data for errors
strText = Space(lStringBuffer) ' BUFFER
intLen = GetPrivateProfileString(strSection, strKeyName, "", _
strText, Len(strText), strIniFile)
If intLen > -1 Then
strText = Left(strText, intLen)
Else
MsgBox "Error into INI file"
Exit Function
End If
ReadINI = strText
' USAGE: MyProperty = ReadINI(Section, KeyName, INIFileName, buffer)
' SAMPLE: Me.FontName = ReadINI("Font", "FontName", "Banner.ini", 255)
End Function
' writes an INI file in the same directory of the .exe file (if another
' dir is not specified); if the file doesn't exist, it creates a new one.
Public Sub WriteINI(strSection As String, strKeyName As String, _
strText As String, strFileName As String, Optional iniDirPathName As String)
' WARNING: when executed within VB IDE, the running exe file is VB6.exe,
' so the INI file is default created into VB6.exe directory, if nothing else is specified
Dim intLen As Integer
Dim strIniFile As String
' set INI file default directory
If iniDirPathName = "" Then iniDirPathName = App.Path
' cut off the final backslash char from path
If Right$(iniDirPathName, 1) = "" Then
Mid$(iniDirPathName, Len(iniDirPathName), 1) = Space$(1)
iniDirPathName = RTrim$(iniDirPathName)
End If
' build INI file's complete path
strIniFile = iniDirPathName & "" & strFileName
' writes into the INI file
intLen = WritePrivateProfileString(strSection, strKeyName, _
strText, strIniFile)
' USAGE: Call WriteINI(Section, KeyName, ValueToWrite, INIFileName)
' SAMPLE: Call WriteINI("Font", "FontName", Me.FontName, "Banner.ini")
End Sub |
Hope it will be useful. 
[Edited by yronium on 08-01-2009 at 08:25 PM GMT]
____________________________
Real Programmer can count up to 1024 on his fingers
|