INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

how can I read and write to an inifile using API calls by Baldwin
Posted: 21 Dec 02

basically an INI file looks like:
[Section]
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:
[MySection]
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)

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close