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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading from an ini file VFP6 2

Status
Not open for further replies.

webmonger

Programmer
Mar 13, 2004
66
AU
I have been trying the method shown in the FAQ to read from an ini file but I do not get a result.

Code:
Declare Integer GetPrivateProfileString in Kernel32 ;
string, string, string, string @, integer, string

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

is this valid for VFP6?
is there another method?
 

You could use the filetostr() function to get contents of an ini file to a character string.

Eg:

cValue=FILETOSTR("MyIniFile.ini")

?cValue

 
Declarations:

Procedure udp_DeclareDLL
Local Array laDllarray(1)
Adlls(laDllarray)
If Ascan(laDllarray,"GetPrivateProfileString") = 0
Declare Integer GetPrivateProfileString In Win32API As GetPrivStr ;
String cSection, String cKey, String cDefault, String @cBuffer, ;
Integer nBufferSize, String posfile
Endif
If Ascan(laDllarray,"WritePrivateProfileString") = 0
Declare Integer WritePrivateProfileString In Win32API As WritePrivStr ;
String cSection, String cKey, String cValue, String posfile
Endif
Release laDllarray
Endproc

Finctions:

Function udf_GetValueFromIniFile
Lparameters lpcFilename, lpcSection, lpcItem
Local getstring, retval
If !File(lpcFilename,1)
Return .F.
Endif
getstring = Space(120) + Chr(0)
retval = ;
GetPrivStr(;
lpcSection,lpcItem, "" + Chr(0), ;
@getstring, ;
Len(getstring), ;
lpcFilename;
)
getstring = Alltrim(Strtran(getstring,Chr(0),""))
If Len(getstring) > 0
Return Evaluate(getstring)
Else
Return ""
Endif
Endfunc

Using (for example):

do udp_DeclareDLL
Local lcIniFileName, lcValueFromIniFile
lcIniFileName=some_path+"userpref.ini"

If File(lcIniFileName,1)
lcValueFromIniFile = ;
udf_GetValueFromIniFile(lcIniFileName,"UserSelected", "PrefLang")
endif


Juri Shutenko
Municipality of Maardu, Estonia
 
webmonger,
Assuming you have a validly constructed .INI file at the location that addbs(fullpath(curdir()))+"MyIniFile.ini" evaluates to, this should work in VFP 3.0 to 9.0 (beta). It should look like:
...
[MySection]
MyKey = ABC
...

And it will return 'ABC'.

There are any number of ways to read any text file (Low-Level IO, FileToStr(), MODIFY FILE , APPEND FROM into a MEMO field, etc.), it's just this API call is easiest to use when you have a well structured .INI file.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top