xsharp.eu • .ini files - Page 2
Page 2 of 2

.ini files

Posted: Sat Feb 13, 2021 3:18 pm
by Neale
Sorry late reply.
Much appreciate your help, the GetString() Method still is not returning a string from the .INI file ?
I do have a small working sample if required ? showing no return value.

.ini files

Posted: Sat Feb 13, 2021 4:14 pm
by Chris
Hi Neale,

Yes, please send the sample to have a look, including both the ini file and the code you are using!

.ini files

Posted: Sat Feb 13, 2021 5:14 pm
by Neale
IniFileSpec_Test is a XVIDE project using StdSDI as a framework, step through Function SetUpIni() and you will see the vars in the debug window, and the App_Paths.INI will be changed and updated. An empty App_Paths.INI is in IniFileSpec_Test directory.

.ini files

Posted: Sat Feb 13, 2021 6:14 pm
by Chris
Hi Neale,

Ah right, I missed this method indeed. Thanks for the very good sample, here's an updated method code that you can use:

Code: Select all

METHOD GetString( cSection AS STRING, cEntry AS STRING, cDefault AS STRING ) AS STRING 
   LOCAL dwBytes AS DWORD
   LOCAL cString AS STRING

	IF SELF:_lFileExists
		LOCAL p := String2Psz(Space(250)) AS PSZ
		dwBytes := GetPrivateProfileString(String2Psz(cSection), String2Psz(cEntry), String2Psz(cDefault), p, 250, String2Psz(SELF:FullPath))
	   	cString := Left(Psz2String(p), dwBytes)
	ENDIF

RETURN cString

.ini files

Posted: Sat Feb 13, 2021 8:05 pm
by Neale
Thank you Chris, all good.

.ini files

Posted: Sun Feb 14, 2021 6:23 am
by wriedmann
Hi Chris,
would it not be better to use the .NET way of calling the functions?

Code: Select all

[DllImport("kernel32.dll",CharSet:=CharSet.Ansi,EntryPoint:="GetPrivateProfileString")];
static method GetString(lpAppName as string, lpKeyName as string, lpDefault as string, lpBuffer as StringBuilder, nSize as dword, lpFileName as string) as dword pascal

public virtual method GetString( cSection as string, cEntry as string, cDefault as string ) as string
	local cReturn 		as string
	local oReturn 		as System.Text.StringBuilder

	oReturn 			:= System.Text.StringBuilder{ _nStringLen }
	IniFile.GetString( cSection, cEntry, cDefault, oReturn, dword( _nStringLen ), _cFileName )
	cReturn				:= oReturn:ToString()

	return cReturn
I remember that I wrote that code with your help several years ago.

Wolfgang

.ini files

Posted: Sun Feb 14, 2021 8:07 am
by Chris
Hi Wolfgang,

Yeah, absolutely! I just tried to adjust the code this time with the least possible amount of changes, but your way is definitely much more efficient!