When you create some application based on client-server you must create a file on client side to get information about your server. Usually the information like hostname, username, password, port, etc. In some windows application developed nowadays use a file called INI file to put the information above. When the application launch, it will get information from ini file then they used the information to connect to the server.
Ini file is a basic standard of the configuration files commonly in Microsoft Windows. But right now it used in several other platforms. sometimes, file using the INI format will use a different extension, such as .cfg .conf or .txt.
What are the INI format ??
The parameter. Every information need identification name and the value, like example like an country as identification name and Indonesia as the value. The parameter on the INI file is delimited by the equals character ("="). The identification name appear on the left of the equals and the value appear on the right equals. Like this bellow :
country = Indonesia
The Section. The parameters is group in one section. The section begin with section name that put in one line and the name section is signed by square brackets ([ and ]). The section has no end delimiter and one section will end at the next section declaration.
[db] dbhost=localhost dbuser=root dbpassword= dbname=office
The Comments. The comment is using one line, begin with semicolon character (;). The comment usually used to put before the section to place some information about the section.
;db section is use to put database information [db]
How the application write and get INI information ?
In the next I will show you to get/write information to/from the INI file with visual basic 6.0. I will use Microsoft Windows Library called Kernel32. In the Kernel32 library there two fucntion to accessed and modify the information in INI file or file based on INI format.
The function are called GetPrivateProfileString and WritePrivateProfileString. Like the name of function above its functionally use to get/write to INI file. The syntax of two above function are :
BOOL WINAPI WritePrivateProfileString(
__in LPCTSTR lpAppName,
__in LPCTSTR lpKeyName,
__in LPCTSTR lpString,
__in LPCTSTR lpFileName
);
DWORD WINAPI GetPrivateProfileString(
__in LPCTSTR lpAppName,
__in LPCTSTR lpKeyName,
__in LPCTSTR lpDefault,
__out LPTSTR lpReturnedString,
__in DWORD nSize,
__in LPCTSTR lpFileName
);
- lpAppName : section from the INI file
- lpKeyName : a key name from the section of the INI file
- lpString : value of lpKeyname
- lpFileName : path of the INI file
- lpDefault : the default string if the lpKeyName cannot be found in the INI file
- lpReturnedString : pointer buffer that recieves the string of the value
- nSize : size of the lpReturnedString buffer
After you create a visual basic project, declare those two function in visual basic module or form to using the function from the Kernel32 library.
Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationname As String, _
ByVal lpKeyName As Any, _
ByVal lsString As Any, _
ByVal lplFilename As String _
) As Long
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
After that, you can using the function to write and get the values of key parameter from INI file. To write the value just call the WritePrivateProfileString and to get the valur just call GetPrivate ProfileString. Bellow the example of two above.
WritePrivateProfileString
Dim lngResult as Long
lngResult = WritePrivateProfileString("biodata", _
"name", "firnas", "test.ini")
The line above will make a "name" parameter and the value will be "firnas" in the biodata section from the test.ini file like this bellow :
[biodata] name=firnas
GetPrivateProfileString
Dim lngResult as Long
Dim strResult As String * 50
lngResult = GetPrivateProfileString("biodata", _
"name", "test.ini", strResult, Len(strResult), _
"test.ini")
The line above will get the value of "name" parameter in the biodata section from test.ini file and put the value in the strResult variable.
In the two example above we use the lngResult variable to put the result of the two function. Its just optional, because we need one variable to put the result of those function.
-Nz-

