dmitrybv 3 Posted October 8 (edited) I wonder why TIniFile.ReadString sets the maximum value size to 2047 without checking the actual value in the file and without limiting the value size in TIniFile.WriteString. Stackoverflow accepted the maximum allowed value to be 65,535 https://stackoverflow.com/questions/10507927/getprivateprofilestring-buffer-length https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestringw DWORD GetPrivateProfileStringW( [in] LPCWSTR lpAppName, [in] LPCWSTR lpKeyName, [in] LPCWSTR lpDefault, [out] LPWSTR lpReturnedString, [in] DWORD nSize, [in] LPCWSTR lpFileName ) ... If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one. But if the developers of System.IniFiles.TIniFile implemented a limit of 2047, then why did they do it only for TIniFile.ReadString (without throwing an exception when exceeded), but not for TIniFile.WriteString. unit System.IniFiles ... function TIniFile.ReadString(const Section, Ident, Default: string): string; var Buffer: array[0..2047] of Char; begin SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section), MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer), MarshaledString(FFileName))); end; procedure TIniFile.WriteString(const Section, Ident, Value: string); begin if not WritePrivateProfileString(MarshaledString(Section), MarshaledString(Ident), MarshaledString(Value), MarshaledString(FFileName)) then raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]); end; Edited October 8 by dmitrybv 2 Share this post Link to post
MarkShark 27 Posted October 9 Interesting! I did not realize that that limit still existed. Here's a stack overflow link that discusses it and some solutions: Delphi INI readstring limitation. Share this post Link to post
dummzeuch 1506 Posted October 9 On 10/8/2024 at 3:36 AM, dmitrybv said: But if the developers of System.IniFiles.TIniFile implemented a limit of 2047, then why did they do it only for TIniFile.ReadString (without throwing an exception when exceeded), but not for TIniFile.WriteString. They probably thought that 2047 was "enough for everybody" and didn't think of it as a limit, so they didn't impose a limit on WriteString either. But we can only guess. OTOH nobody(*1) uses TIniFile any more but TMemIniFile because it overcomes all the limitations of Get/SetPrivateProfileString so that's a moot point. (*1 for suitable definitions of nobody ) 3 Share this post Link to post