Solved

Registry Problem

Posted on 2002-04-05
8
170 Views
Last Modified: 2010-04-04
I have A problem with the following code. this code is not retrieving me the data. when i look at the path through regedit i can see the reg values r there. wheather i need to refresh  or what should i do


procedure TForm1.Button1Click(Sender: TObject);
var
  sRegSubKey : string;
  vRegistry : TRegistry;
  Version,databaseNameT : string;

begin
  try
    try
      sRegSubKey := 'Path to the reg file';
      vRegistry := TRegistry.Create;
      vRegistry.RootKey := HKEY_LOCAL_MACHINE;
      {surinscrit la registry si nécessaire, CanCreate=True}
      vRegistry.OpenKey(sRegSubKey, True);
      with vRegistry do
      begin
        Version := ReadString('Version');
        databaseNameT:=ReadString('DatabaseName');
      end; {with}

    finally
     vRegistry.free;
    end;

    Label1.Caption := 'Release '+Version;
    Label2.Caption:='DatbaseName '+databaseNameT;
  except
    On E:Exception do
    begin
      ShowMessage('excep');
    end;
  end;
end;
0
Comment
Question by:jonathan2002
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 6922525
HI,jonathan2002,
You are not allowed to create Key as you want.
What you can do is:
sRegSubKey := 'SoftWare\Path to the reg file';
...
or
sRegSubKey := 'SoftWare\Path to the reg file';
...
vRegistry.RootKey := HKEY_CURRENT_USER;
...
And one more remark.
You use the block try-finally-end in not quite proper way.
Sincerely,
Nestorua.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6922528
Sorry,
In the second way it is:
or
sRegSubKey := 'Path to the reg file';
...
vRegistry.RootKey := HKEY_CURRENT_USER;Sincerely,
Nestorua.
0
 
LVL 1

Accepted Solution

by:
OryxConLara earned 25 total points
ID: 6923533
jonahat2002--

I'll assume that when you wrote "'Path to the reg file'" you meant something like "SOFTWARE\Borland\Borland Shared\DBExpress", and just didn't want to bother us with the details :).

One problem may be that the Version and Databasename keys aren't strings, but some other type. What type of key does Regedit show for these two values of the key? If it's not REG_SZ, then these aren't stored as strings, and we'll have to do something else to extract this info. If you can tell me what kind they are, I think I may be able to help you further.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 17

Assisted Solution

by:geobul
geobul earned 25 total points
ID: 6923715
Hi,

try:
vRegistry.Access := KEY_READ;
vRegistry.OpenKey(sRegSubKey, false);

perhaps you have 'read only' access to that key.

Regards, Geo
0
 
LVL 2

Expert Comment

by:gallaghe
ID: 6928247
You should first check to see if the key exists, then open it Read-Only if under HKLM i.e.

   if vRegistry.OpenKey(sRegSubKey, False) then
   begin
      ...
   or

   if vRegistry.OpenKeyReadOnly(sRegSubKey) then
   begin
      ...


Also when getting a value use
   if vRegistry.ValueExists(sRegSubKey) then
      Version := ReadString('Version');
      ...

never assume the value exists.
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9002927
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between OryxConLara and gallaghe

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 17

Expert Comment

by:geobul
ID: 9003078
pnh73,

what new have gallaghe added to this question? There are two possible reasons: either the key is not of string type (pointed out by OryxConLara) or the user has no write access and has to open the key read-only (pointed by me). The original code has try..except block so it's definatelly not necessary to check if the key exists beforehand.

Regards, Geo
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004732
I will leave a recommendation in the Cleanup topic area that this question is:

Split between OryxConLara and geobul

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question