* * *

Author Topic: TRegIniFile raises exception  (Read 1408 times)

BjPascal

  • New member
  • *
  • Posts: 15
TRegIniFile raises exception
« on: July 11, 2018, 03:11:24 pm »
using Lazarus 1.9.0 in Delphi-mode and FPC 3.1.1 on Win7 x64

When using TRegIniFile then the first Readinteger works ok.

reg:= TRegIniFile.Create('\Software\myName\myApp');
i:= reg.ReadInteger(SECT_SETTINGS, 'edRcvUdpPort', 0);
i:= reg.ReadInteger(SECT_SETTINGS, 'edRcvUdpPort', 0);

But the second identical Readinteger generates this
   "Project * raised exception class 'External: ?'"

The same with other read methods. It seems I can use a method only once.
Any idea why and how to fix it?

I imported it from Delphi and don't want to modify the source.


Trenatos

  • Sr. Member
  • ****
  • Posts: 442
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: TRegIniFile raises exception
« Reply #1 on: July 11, 2018, 05:12:47 pm »
http://wiki.freepascal.org/Using_INI_Files

The wiki for FPC shows TINIFile.Create(); rather than TRegIniFile.Create();

BjPascal

  • New member
  • *
  • Posts: 15
Re: TRegIniFile raises exception
« Reply #2 on: July 11, 2018, 06:56:19 pm »
I use Registry acces with the option to easily switch to ini files if needed.

Trenatos

  • Sr. Member
  • ****
  • Posts: 442
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: TRegIniFile raises exception
« Reply #3 on: July 11, 2018, 07:27:17 pm »

BjPascal

  • New member
  • *
  • Posts: 15
Re: TRegIniFile raises exception
« Reply #4 on: July 12, 2018, 01:25:37 am »
I now found the error:
each method-call closes the key. So a subsequent call returns with "handle is invalid".
I fixed regini.inc at the bottom:

procedure TRegIniFile.CloseSection;
begin
//  CloseKey(CurrentKey);
end;

and it now works fine!

Bart

  • Hero Member
  • *****
  • Posts: 3127
    • Bart en Mariska's Webstek
Re: TRegIniFile raises exception
« Reply #5 on: July 12, 2018, 06:44:53 pm »
Worsk fine (without your patch) for me (win32 fpc 3.04 and 3.1.1).

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 7130
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

Bart

  • Hero Member
  • *****
  • Posts: 3127
    • Bart en Mariska's Webstek
Re: TRegIniFile raises exception
« Reply #7 on: July 12, 2018, 08:20:22 pm »
Tested with fpc 3.0.4 win32 and win64 and win fpc trunk (r39352) win32:

Code: Pascal  [Select]
  1. program rtest;
  2.  
  3. {$apptype console}
  4. {$ifdef fpc}{$mode objfpc}{$endif}
  5.  
  6. uses
  7.   registry, sysutils;
  8.  
  9. var
  10.   RegIni: TRegIniFile;
  11.   WFileName: String;
  12.   i: Integer;
  13. begin
  14.   RegIni := TRegIniFile.Create(
  15.       'Software\MicroSoft\Windows\CurrentVersion\Explorer');
  16.   try
  17.     for i := 1 to 3 do
  18.     begin
  19.       WFileName := RegIni.ReadString ('Shell Folders', 'Desktop', 'Failure!');
  20.       writeln(format('Try %d: WFileName = "%s"',[i,WFileName]));
  21.     end;
  22.   finally
  23.     RegIni.Free;
  24.   end;
  25. end.

Outputs

Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\Console\regini>rtest
Try 1: WFileName = "C:\Users\Bart\Desktop"
Try 2: WFileName = "C:\Users\Bart\Desktop"
Try 3: WFileName = "C:\Users\Bart\Desktop"

Bart

BjPascal

  • New member
  • *
  • Posts: 15
Re: TRegIniFile raises exception
« Reply #8 on: July 13, 2018, 10:25:27 am »
Yes Bart that works.

But not this, where the "Section"-name has been moved to the create path to keep subsequent calls of the same section simpler.
Code: Pascal  [Select]
  1. RegIni := TRegIniFile.Create('Software\MicroSoft\Windows\CurrentVersion\Explorer\Shell Folders');
  2.  for i := 1 to 3 do
  3.      WFileName := RegIni.ReadString ('', 'Desktop', 'Failure!');
  4.  

wp

  • Hero Member
  • *****
  • Posts: 5043
Re: TRegIniFile raises exception
« Reply #9 on: July 13, 2018, 10:45:20 am »
Yes Bart that works.

But not this, where the "Section"-name has been moved to the create path to keep subsequent calls of the same section simpler.
Code: Pascal  [Select]
  1. RegIni := TRegIniFile.Create('Software\MicroSoft\Windows\CurrentVersion\Explorer\Shell Folders');
  2.  for i := 1 to 3 do
  3.      WFileName := RegIni.ReadString ('', 'Desktop', 'Failure!');
  4.  
But RegIniFile still is an IniFile after all. Would you do the same with TIniFile?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Bart

  • Hero Member
  • *****
  • Posts: 3127
    • Bart en Mariska's Webstek
Re: TRegIniFile raises exception
« Reply #10 on: July 13, 2018, 02:48:31 pm »
But RegIniFile still is an IniFile after all. Would you do the same with TIniFile?

It may not be best practice, but it should not crash.
TRegIniFile is a derivative of TRegistry, not of TInifile b.t.w.
It should be perfectly OK to query register values multiple times.

B.t.w. I was unaware of the fact you could leave Section parameter empty.
Is that officially supported?

Bart
« Last Edit: July 13, 2018, 02:50:50 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 3127
    • Bart en Mariska's Webstek
Re: TRegIniFile raises exception
« Reply #11 on: July 13, 2018, 04:34:30 pm »
Even with Section parameter empty I still don't get a crash (but on second try reading fails).
Delphi (7) seems to allow that and consecutive reads are OK.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 3127
    • Bart en Mariska's Webstek
Re: TRegIniFile raises exception
« Reply #12 on: July 13, 2018, 04:49:44 pm »
Reported as issue 33980.

Bart

wp

  • Hero Member
  • *****
  • Posts: 5043
Re: TRegIniFile raises exception
« Reply #13 on: July 13, 2018, 05:07:04 pm »
TRegIniFile is a derivative of TRegistry, not of TInifile b.t.w.
I do remember that in my Delphi days I had replaced TIniFile by TRegIniFile in order to easily store the settings to the registry without changing anything else. This must be the reason why I confused it.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

BjPascal

  • New member
  • *
  • Posts: 15
Re: TRegIniFile raises exception
« Reply #14 on: July 15, 2018, 10:33:40 am »
Bart,

I have been using the empty section from Delphi-5 to Berlin, simply because it is much clearer to have the registry tree defined in one single constant instead of breaking it up into two. And TRegini has been my favourite because it's a lot simpler to use than anything else.

I found another bug:
When using
TRegIniFile.Create('\Software\myName\myApp');

(with leading backslash) then all write operations will fail, because these always prepend another '\' to it.
The read functions don't have that issue.

Delphi treats both cases properly.
So you might add this to your issue report also.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus