
Originally Posted by
batori
this code does work but it does not give back the entire string form the ini...one last char is missing most of the times. ..
The problem here is that if more than 254 characters were written to the INI then the ReadINI function will truncate to 254 characters, as the function is written. Also the return value (if successful) is not one less than the length of the string, so subtracting 1 from it is inappropriate.
The API returns Len(String)-1 if Section & Key parameters are passed and the string sent to the API isn't big enough for the returned value. The workaround is to test the return value and resize the passed string until the API return value meets a specific criteria...
Code:
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String, v As Long
Dim retLen As Long
Do
retLen = retLen + 260 ' arbitrary - can be set higher/lower if desired
RetVal = Space(retLen)
v = GetPrivateProfileString(Section, Key, "", RetVal, retLen, Filename)
Loop Until v < retLen - 1
ReadIni = Left$(RetVal, v)
End Function