Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I read and write to an inifile using API calls

API Functions

how can I read and write to an inifile using API calls

by  Baldwin  Posted    (Edited  )
basically an INI file looks like:
[ignore][Section][/ignore]
key=value

You need two functions to write to and read from an inifile

Those functions are:
GetPrivateProfileString and
WritePrivateProfileString

Both reside in the Kernel32

WritePrivateProfileString has 4 parameters
cSectionName
cKeyname
cValueToStore
cPathAndnameOfIniFile

GetPrivateProfileString has 6 parameters
cSectionName
cKeyname
cDefaultReturnValue
cBufferWhereReadValueIsStored
nLengthOfBuffer
cPathAndnameOfIniFile
It returns an integer, deining the number of read characters.

Therefore the syntax to declare either function is
Declare Integer GetPrivateProfileString in Kernel32 ;
string, string, string, string @, integer, string

DECLARE integer WritePrivateProfileString in Kernel32 ;
string, string, string, string

To place a value in the inifile (assumed MyIniFile.ini in the current directory)
?WritePrivateprofileString("MySection","MyKey","Key1Value",addbs(fullpath(curdir()))+"MyIniFile.ini")

if a 1 is returned, writing was succesful, a 0 (zero) indicates a failure. The integer is actually a boolean value.
Out of the blue, automagically there is a MyIniFile.ini in the directory, created by this call.

It should look like:
[ignore][MySection][/ignore]
Mykey=Key1Value

To read the value:
lcBuffer = space(255)
?GetPrivateProfileString( "MySection","MyKey","",@lcBuffer, Len(lcBuffer), addbs(fullpath(curdir()))+"MyIniFile.ini")

The returned value indicates the number of bytes read.
There are two more things we need to do:
Strip of the trailing chr(0) with
lcBuffer = strtran(lcBuffer,chr(0),"")
and trim it
lcBuffer = alltrim(lcBuffer)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top