|
Reported by Alan M. Cohen; checked by Reinier Sterkenburg
Extra information on this 'bug' was provided by Jordan Russell, Eddie Churchill (Borland), and Erwin Dokter
See also bug #676.
The bug can be reliably reproduced with the following code attached
to a button click:
IniFile := TIniFile.Create('Test.ini');
IniFile.WriteString('Test ', 'Key1', 'Val1');
IniFile.Free;
Note that the last character in the 'Test ' argument on the second line is a
' '. Deleting the ' ' ends the problem.
Erwin Dokter explains:
The fact this bug does not appear in Delphi 1 is because Delphi 1 does not
use the PChar typecast. In Delphi 1, all calls to an API function which
requires PChar parameters used the following method to convert a string to a
PChar:
function SomeCall(const parm: string): Word;
var
buf: array[0..255] of Char;
begin
SomeApiCall(StrPCopy(buf, parm);
end;
In Delphi 2, the PChar typecast was introduced, and all calls changed to:
function SomeCall(const parm: string): Word;
begin
SomeApiCall(PChar(parm));
end;
This explains the manifestation in Delphi 2 onward. The page fault occurs
because WritePrivateProfileString tries to strip the trailing space from a
typecasted constant. Passing a real PChar, as in Delphi 1, poses no problem.
Some other notes:
- This bug is Windows version dependent: Windows 95 and 98 have the bug,
but NT 4.0 SP3 does not have it (although there the space gets
truncated from the section name). The bug does not appear in NT because WritePrivateProfileString uses its own buffer.
- TIniFile.WriteBool inherits this bug because it calls WriteString.
- WriteString uses WritePrivateProfileString and that in turn seems to
indicate (according to MSDN) that spaces are not allowed in the first param.
Mind you it actually says that it can contain uppercase and lowercase
letters but numbers seem to work as well.
While Delphi should strip out any spaces before calling
WritePrivateProfileString (and who knows that is what it may do in the future)
for the time being users should avoid using spaces.
|