Page 1 of 1

IniRead (AHK v2): spaces as default value

Posted: 21 Sep 2018, 15:48
by jeeswg
In AHK v2 it appears that if you specify a default value of space or multiple spaces, a blank string is returned.

Code: Select all

;tested on AHK v2.0-a099
vPath := ""
MsgBox("[" IniRead(vPath, "Section", "NonExistentKey", A_Space) "]") ;[]
MsgBox("[" IniRead(vPath, "Section", "NonExistentKey", A_Space A_Space A_Space) "]") ;[]
MsgBox("[" IniRead(vPath, "Section", "NonExistentKey", "default") "]") ;[default]

Re: IniRead (AHK v2): spaces as default value

Posted: 21 Sep 2018, 19:53
by lexikos
This is standard Windows behaviour. IniRead when specifying a key is literally just a call to GetFullPathName and a call to GetPrivateProfileString. Everything is handled by the latter function, including returning a default value if the key wasn't found.

Re: IniRead (AHK v2): spaces as default value

Posted: 22 Sep 2018, 01:21
by jeeswg
Thanks, that also explains the curious IniRead (AHK v1) behaviour.

GetPrivateProfileString function | Microsoft Docs
https://docs.microsoft.com/en-gb/window ... filestring
Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.
Note: GetPrivateProfileString appears to trim trailing spaces but not tabs or any other characters.

Code: Select all

;test IniRead default strings (tested on AHK v2.0-a099)
vPath := "", vOutput := ""
Loop 65535
{
	vDefault := "abc" Chr(A_Index)
	vRet := IniRead(vPath, "Section", "NonExistentKey", vDefault)
	if !(vDefault = vRet)
		vOutput .= A_Index "`r`n"
}
Clipboard := vOutput
MsgBox(vOutput) ;32 ;only trailing spaces appear to be trimmed
return