View Issue Details

IDProjectCategoryView StatusLast Update
0036663FPCFCLpublic2020-03-04 10:03
ReporterWallaby Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformWin32OSWindows 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0036663: Broken TRegistryIniFile
DescriptionThe following code works incorrectly in trunk and fixes_3.2:

{$MODE DELPHI}

program test;

uses
  Registry;

const
  RegPath = 'Software\Company Name\Product Name';

begin
  {Write}
  with TRegistryIniFile.Create(RegPath) do
  try
    WriteString('Test', 'Key', 'Value');
  finally
    Free;
  end;
  {Read}
  with TRegistryIniFile.Create(RegPath) do
  try
    WriteLn(ReadString('Test', 'Key', 'Duh!'));
  finally
    Free;
  end;
end.

Instead of creating: HKEY_CURRENT_USER\Software\Company Name\Product Name
It creates HKEY_CURRENT_USER\Software\Company Name\Product Name\Software\Company Name\Product Name\Test

On top of that no value is written. The above code will print "Duh!" rather than "Value".

As far as I can tell it's related to converting TRegistry to Unicode. It would be great to have this fixed before 3.2 release as this will break many programs.
Steps To ReproduceCompile and execute the attached program. Open regedit.exe, navigate to HKEY_CURRENT_USER\Software\Company Name and expand it all the way down.

Note that the path is wrong and no value was written.
TagsRegistry
Fixed in Revision44127
FPCOldBugId
FPCTarget3.2.0
Attached Files

Activities

Wallaby

2020-02-06 07:18

reporter  

test.pas (411 bytes)   
{$MODE DELPHI}

program test;

uses
  Registry;

const
  RegPath = 'Software\Company Name\Product Name';

begin
  {Write}
  with TRegistryIniFile.Create(RegPath) do
  try
    WriteString('Test', 'Key', 'Value');
  finally
    Free;
  end;
  {Read}
  with TRegistryIniFile.Create(RegPath) do
  try
    WriteLn('Read: ', ReadString('Test', 'Key', 'Duh!'));
  finally
    Free;
  end;
end.
test.pas (411 bytes)   
reg-err-path.png (30,778 bytes)   
reg-err-path.png (30,778 bytes)   

Bart Broersma

2020-02-06 22:36

reporter   ~0120909

Last edited: 2020-02-06 22:36

View 2 revisions

> As far as I can tell it's related to converting TRegistry to Unicode.
That doesn't seem very likely.

TRegIniFile was already broken long before I rewrote TRegistry to be fully unicode.
See e.g 0035022 (TRegIniFile writes to the wrong key)
Please apply registry.currentpath.diff and then reginifile.opensection.diff in that report and see if that fixes the issue.

> as this will break many programs.
TRegIniFile was introduced to make porting applications from using INI files to use the Windows registry easy, I believe when Win95 came out in the era of D2 (?).
If you insist on using the registry, then moving to using TRegistry was the way to go (you've had > 20 years to adapt your code).

Bart Broersma

2020-02-06 22:41

reporter   ~0120910

TRegistryIniFile simply encapsulates a TRegIniFile b.t.w. Hence my reference to TRegIniFile.

Bart Broersma

2020-02-07 08:34

reporter   ~0120912

> It would be great to have this fixed before 3.2 release
It's a regression, so it has to be fixed before 3.2
Probably I broke it, so I'll try to fix it.

> The above code will print "Duh!" rather than "Value"
ReadString tries to read from the correct path (WriteString wrote to the wrong path), so it obviouslly cannot find the correct result.

Bart Broersma

2020-02-07 18:24

reporter   ~0120922

Indeed it was my fault. I messed up handling of Key in the Windows implementation of sysCreateKey in r41784.
Uploaded patch.
registry.syscreatekey.diff (728 bytes)   
Index: packages/fcl-registry/src/winreg.inc
===================================================================
--- packages/fcl-registry/src/winreg.inc	(revision 43847)
+++ packages/fcl-registry/src/winreg.inc	(working copy)
@@ -59,12 +59,13 @@
   Disposition: Dword;
   Handle: HKEY;
   SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
+  U: UnicodeString;
 
 begin
   SecurityAttributes := Nil;
-  Key:=PrepKey(Key);
+  U:=PrepKey(Key);
   FLastError:=RegCreateKeyExW(GetBaseKey(RelativeKey(Key)),
-                              PWideChar(Key),
+                              PWideChar(U),
                               0,
                               '',
                               REG_OPTION_NON_VOLATILE,
registry.syscreatekey.diff (728 bytes)   

Bart Broersma

2020-02-07 18:35

reporter   ~0120923

@Wallaby: please test with attached patch.
I asked on the devel ML to have it applied ASAP.

Michael Van Canneyt

2020-02-07 18:48

administrator   ~0120924

Applied patch from Bart, thank you very much.

Issue History

Date Modified Username Field Change
2020-02-06 07:18 Wallaby New Issue
2020-02-06 07:18 Wallaby File Added: test.pas
2020-02-06 07:18 Wallaby File Added: reg-err-path.png
2020-02-06 07:52 Wallaby Tag Attached: Registry
2020-02-06 22:36 Bart Broersma Note Added: 0120909
2020-02-06 22:36 Bart Broersma Note Edited: 0120909 View Revisions
2020-02-06 22:41 Bart Broersma Note Added: 0120910
2020-02-07 08:34 Bart Broersma Note Added: 0120912
2020-02-07 18:24 Bart Broersma File Added: registry.syscreatekey.diff
2020-02-07 18:24 Bart Broersma Note Added: 0120922
2020-02-07 18:35 Bart Broersma Note Added: 0120923
2020-02-07 18:48 Michael Van Canneyt Assigned To => Michael Van Canneyt
2020-02-07 18:48 Michael Van Canneyt Status new => resolved
2020-02-07 18:48 Michael Van Canneyt Resolution open => fixed
2020-02-07 18:48 Michael Van Canneyt Fixed in Version => 3.3.1
2020-02-07 18:48 Michael Van Canneyt Fixed in Revision => 44127
2020-02-07 18:48 Michael Van Canneyt FPCTarget => 3.2.0
2020-02-07 18:48 Michael Van Canneyt Note Added: 0120924