Amos Amos - 4 months ago 52
Ini Question

Delphi INI readstring limitation

I'm storing my application's settings in an INI file. I read that there is a limitation of 2kb for a binary entry so I encoded the binary into a string and stored the value as string (writestring). When checking the file, it seems that all the string was stored as expected.

When trying to read it back, it seems that only 2047 characters were read so when decoding it back to a stream, it fails.

Apparently it seems there is a 2kb limitation for string as well but I was wondering if that is that or maybe I did something wrong. If there's such a limitation, any idea how can I bypass it?

Thanks

EDIT:
silly me, I went to system.inifiles and it says in the code

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;

Answer

The solution is easy.

Extend TInifile and insert your own version of ReadString.

TMyIniFile = class(TInifile)
      function ReadString(const Section, Ident, Default: string): string; override;
end;

function TMyIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..largenumber] of Char;
begin                                
  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;
Comments