TRegIniFile.WriteString writes to wrong Key
Original Reporter info from Mantis: Bart @flyingsheep
-
Reporter name: Bart Broersma
Original Reporter info from Mantis: Bart @flyingsheep
- Reporter name: Bart Broersma
Description:
Whilst trying to see if I could fix #33980 (closed) I stumbled uopn this bug.
Consider this piece of code that simply tries to create some registry entries:
function CreateTestEntry: Boolean;
const
BoolStr: array[Boolean] of String = ('False','True');
OKStr: array[Boolean] of String = ('FAIL','OK');
var
RegIni: TRegIniFile;
B: Boolean;
function TryOpenKey(Key: String; CanCreate: Boolean): Boolean;
begin
Result := RegIni.OpenKey(Key, CanCreate);
writeln(format('OpenKey(''%s'',%s): %s',[Key,BoolStr[CanCreate],OkStr[Result]]));
end;
function TryWriteString(Section, Ident, Value: String): Boolean;
begin
Result := False;
try
RegIni.WriteString(Section, Ident, Value);
Result := True;
writeln(format('WriteString(''%s'',''%s'',''%s''): %s',[Section,Ident,Value,OkStr[Result]]));
except
on E: Exception do
writeln(format('WriteString(''%s'',''%s'',''%s''): %s',[Section,Ident,Value,E.Message]));
end;
end;
begin
Result := False;
RegIni := TRegIniFile.Create('\Software');
try
writeln('CurrentPath=',RegIni.CurrentPath);
B := RegIni.CreateKey('FPCTEST');
writeln('CreateKey=',B);
if not B then Exit;
if not TryOpenKey('FPCTEST',False) then Exit;
writeln('CurrentPath=',RegIni.CurrentPath);
if not TryOpenKey('RegIni',True) then Exit;
writeln('CurrentPath=',RegIni.CurrentPath);
// should create \Software\FPCTEST\RegIni\Strings\String1 with value Value1
// FPC creates \Software\Strings\String1 with Value1
if not TryWriteString('Strings','String1','Value1') then Exit;
// should create \Software\FPCTEST\RegIni\Strings\String2 with value Value2
// FPC creates \Software\Strings\String1 with Value1
if not TryWriteString('Strings','String2','Value2') then Exit;
// should create \Software\FPCTEST\RegIni\String1 with value Value1
// gives Exception in FPC: ERegistryException: Failed to set data for value "String1"
if not TryWriteString('','String1','Value1') then Exit;
if not TryWriteString('','String2','Value2') then Exit;
Result := True;
finally
RegIni.Free;
end;
end;
var
B: Boolean;
begin
B := CreateTestEntry;
writeln('CreateTestEntry=',B);
end.
Steps to reproduce:
Build and run attached program on Windows.
Then open the registry editor.
Notice that the WriteString('Strings','String1','Value1') creates the Ident/Value pair in \Software\Strings, while it should create them in \Software\FPCTEST\RegIni
(Delphi 7 puts them in the expected place)
Notice also that the program fails to do a WriteString() with '' as Section parameter.
Further notice that the value for CurrentPath is broken (it missed the separating backslashes).
See output with Delphi7 versus Fpc trunk:
C:\Users\Bart\LazarusProjecten\bugs\Console\regini>dcc32 rtest.lpr
Borland Delphi Version 15.0
...
154 lines, 0.03 seconds, 76944 bytes code, 4153 bytes data.
C:\Users\Bart\LazarusProjecten\bugs\Console\regini>rtest
CurrentPath=Software
CreateKey=TRUE
OpenKey('FPCTEST',False): OK
CurrentPath=Software\FPCTEST
OpenKey('RegIni',True): OK
CurrentPath=Software\FPCTEST\RegIni
WriteString('Strings','String1','Value1'): OK
WriteString('Strings','String2','Value2'): OK
WriteString('','String1','Value1'): OK
WriteString('','String2','Value2'): OK
CreateTestEntry=TRUE
C:\Users\Bart\LazarusProjecten\bugs\Console\regini>fpc rtest.lpr
Free Pascal Compiler version 3.3.1 [2019/01/02] for i386
...
152 lines compiled, 0.2 sec, 158992 bytes code, 6692 bytes data
C:\Users\Bart\LazarusProjecten\bugs\Console\regini>rtest
CurrentPath=Software
CreateKey=TRUE
OpenKey('FPCTEST',False): OK
CurrentPath=SoftwareFPCTEST
OpenKey('RegIni',True): OK
CurrentPath=SoftwareFPCTESTRegIni
WriteString('Strings','String1','Value1'): OK
WriteString('Strings','String2','Value2'): OK
WriteString('','String1','Value1'): Failed to set data for value "String1"
CreateTestEntry=FALSE
Mantis conversion info:
- Mantis ID: 35022
- OS: Windows
- OS Build: 10
- Build: r40745
- Platform: i386
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 44477 (#25b7e135)
- Monitored by: » mftq75 (C Western)
- Target version: 3.2.0