Recent

Author Topic: Tregistry Problem (Solved)  (Read 4232 times)

codeman

  • Jr. Member
  • **
  • Posts: 78
Tregistry Problem (Solved)
« on: June 25, 2012, 08:06:40 pm »
Hello , im trying to read a reg. entry , but i get an empty msgbox ... see below

Should it work ? (i tested on WinXP SP3 x32)

Code
Code: [Select]
reg := TRegIniFile.Create('');
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.ReadString('SOFTWARE\Microsoft\Windows NT\CurrentVersion','ProductName',str);
reg.free;
showmessage(str);     
« Last Edit: June 25, 2012, 10:07:33 pm by codeman »
Lazarus v2.02 Win10

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Tregistry Problem
« Reply #1 on: June 25, 2012, 08:29:00 pm »
IIRC you need to do 'OpenKey' before reading values...

Gr.
Dirk.

codeman

  • Jr. Member
  • **
  • Posts: 78
Re: Tregistry Problem
« Reply #2 on: June 25, 2012, 09:35:21 pm »
I modified it but it does not seem to work again  :( ...  (i tried 'openkey function' too)
Also i get a compile error : wrong number of parameters for call to 'ReadString'   



Code: [Select]
reg := TRegIniFile.Create;
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKeyreadonly('SOFTWARE\Microsoft\Windows NT\CurrentVersion');
str:=reg.ReadString('ProductName');
reg.closekey;
reg.free;
showmessage(str);
Lazarus v2.02 Win10

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Tregistry Problem
« Reply #3 on: June 25, 2012, 09:49:57 pm »
Just use TRegistry, that should worl:
Code: [Select]
var reg: TRegistry;
    str: string;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKeyreadonly('SOFTWARE\Microsoft\Windows NT\CurrentVersion');
  str := reg.ReadString('ProductName');
  reg.closekey;
  reg.free;
  showmessage(str);
end;

Or use the highly undesired code like below.
It seems to work but it shouldn't:
Code: [Select]
var reg: TRegIniFile;
    str: string;
begin
  reg := TRegIniFile.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKeyreadonly('SOFTWARE\Microsoft\Windows NT\');
  str := reg.ReadString('CurrentVersion', 'ProductName', '<unknown>');
  reg.closekey;
  reg.free;
  showmessage(str);
end; 
« Last Edit: June 25, 2012, 09:55:11 pm by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

codeman

  • Jr. Member
  • **
  • Posts: 78
Re: Tregistry Problem
« Reply #4 on: June 25, 2012, 09:54:36 pm »
LOL .. its working   %)

Thanks   8)
Lazarus v2.02 Win10

 

TinyPortal © 2005-2018