Alot of people wanted to know how they could use INI files with there applications. So here it is. This is the best way I can show a beginner. First you will need to declare two functions.
Declare Functions:
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
Declare Function WritePrivateProfileString Lib “KERNEL32″ _
Alias “WritePrivateProfileStringA” (ByVal lpApplicationName _
As String, ByVal lpKeyName As String, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Now you need to create two custom functions to accompany the Decalres.
Function ReadINI(Section, KeyName, filename As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section, ByVal _
KeyName, “”, sRet, Len(sRet), filename))
End Function
Function WriteINI(sSection As String, sKeyName As String, _
sNewString As String, sFileName) As Integer
Dim r
r = WritePrivateProfileString(sSection, sKeyName, _
sNewString, sFileName)
End Function
Now that you have that set up, here is an example of some data in an INI and how to retrieve and write to it.
INI Data Format
[Main]
Capation=myCaption
Width=5000
Password=true
The text within the [] defines the section of the INI to read from. Data below the Section are called keys. ‘Caption’ is a key in this case. That should sum up an INI file structure. You can have as many sections and keys as you need.
Reading from the INI
Dim myString as String
myString=ReadINI(“Main”,”Caption”, FILENAME)
Normally INI files carry the .ini extension. However you can create the file with any extension you want. Maybe you are not good with encryption and dont want people playing with the INI file. Make the extension .DLL or something that does not have a laymans editor. Its a cheap trick, but it works.
Writting to the INI
WriteINI “Main”,”Caption”,”YouCaption”,FILENAME
Thats not too hard. Just remember that you have to call the right section and key to retrieve your data. You can have section MAIN and section OTHER with both having keys named Caption. But if you call the wrong section, you will get the wrong key.
That should sum things up pretty well. Remember to email me any question or post one here, Im glad to help.
Popularity: 38% [?]