Solved

Ini to registry

Posted on 2002-07-14
5
471 Views
Last Modified: 2010-04-04
Hi all,

I am writing a console application to check registry keys during a logon script.

Most of it is done, but I have a few problems which are most likely going to be easy, but I just cannot seem to fix it.

The syntax of the ini file is as follows

[\Software\Test]
Rootkey=HKEY_CURRENT_USER
Type=REG_SZ
Key=Default
Data=It works

The two problems I have are

Reg.rootkey : if I use reg.rootkey := strtocard(ini.readstring(Header, 'Type', 'HKEY_CURRENT_USER)); it compiles, but does gives an error on reading the ini file.
I know the answer is staring me in the face, but I don't see it...

2nd, what would be the best way to check what type the value is (reg_sz, Reg_dword, Reg_binary)and if that is established to choose the correct ini.readxxxx routine. Right now I only check for strings, but would like to include integer values and binary. Or do I going to have to write 3 instances of ini.readxxx (as in "if string then ini.readstring", "if boolean then ini.readbool" etc..)and use booleans to activate them according to the type stated in the ini, or is there an easier (shorter) way.

I hope I made myself clear here... ;)
Kind regards
Marc
0
Comment
Question by:hellfire052497
5 Comments
 
LVL 1

Expert Comment

by:slavikn
ID: 7152557
Solution for the first problem:

var
   Root: String;
begin
   Root := ini.readstring(Header, 'Type', 'HKEY_CURRENT_USER);

   If Root = 'HKEY_CLASSES_ROOT' Then RegRoot := HKEY_CLASSES_ROOT
      Else If Root = 'HKEY_CURRENT_USER' Then RegRoot := HKEY_CURRENT_USER
         Else If Root = 'HKEY_LOCAL_MACHINE' Then RegRoot := HKEY_LOCAL_MACHINE
            Else If Root = 'HKEY_USERS' Then RegRoot := HKEY_USERS
               Else If Root = 'HKEY_PERFORMANCE_DATA' Then RegRoot := HKEY_PERFORMANCE_DATA
                  Else If Root = 'HKEY_CURRENT_CONFIG' Then RegRoot := HKEY_CURRENT_CONFIG
                     Else If Root = 'HKEY_DYN_DATA' Then RegRoot := HKEY_DYN_DATA
                        Else
                           Begin
                              Result := False;
                              Exit;
                           End;
......
0
 
LVL 1

Expert Comment

by:slavikn
ID: 7152562
Here is the full function:

function GoToPath(var TheReg: TRegistry; Path: string; AllowCreate: Boolean): Boolean;
var
   Root, Key: String;
   RegRoot: HKEY;
   KeyPos: Byte;
begin
   KeyPos := Pos('\', Path) + 1;
   Root := Copy(Path, 1, KeyPos - 2);
   Key := Copy(Path, KeyPos, MaxInt);
   If Root = 'HKEY_CLASSES_ROOT' Then RegRoot := HKEY_CLASSES_ROOT
      Else If Root = 'HKEY_CURRENT_USER' Then RegRoot := HKEY_CURRENT_USER
         Else If Root = 'HKEY_LOCAL_MACHINE' Then RegRoot := HKEY_LOCAL_MACHINE
            Else If Root = 'HKEY_USERS' Then RegRoot := HKEY_USERS
               Else If Root = 'HKEY_PERFORMANCE_DATA' Then RegRoot := HKEY_PERFORMANCE_DATA
                  Else If Root = 'HKEY_CURRENT_CONFIG' Then RegRoot := HKEY_CURRENT_CONFIG
                     Else If Root = 'HKEY_DYN_DATA' Then RegRoot := HKEY_DYN_DATA
                        Else
                           Begin
                              Result := False;
                              Exit;
                           End;
   TheReg.RootKey := RegRoot;
   Result := TheReg.OpenKey(Key, AllowCreate);
end;
0
 
LVL 1

Accepted Solution

by:
OryxConLara earned 200 total points
ID: 7152596
Hi, Marc!

First, when the Hive name is read in from the INI file, it's a string. You need to read this in as a string, then convert to a Hive Constant.

Then, the string in the []'s is an ini section name. Usually what's there is a general classification of what information follows, not your key path. So, in your case you could have

[TestKey1]
Rootkey=HKEY_CURRENT_USER
Path=\Software\Test
Type=REG_SZ
Key=Key1
Data=It works

[TestKey2]
Rootkey=HKEY_CURRENT_USER
Path=\Software\Test
Type=REG_SZ
Key=Key2
Data=It works better

Then you read each section by itself:

procedure ReadSection(sect :string);
var
  hstr :string;
  ini  :TIniFile;
  Reg  :TRegistry;
  Key  :string;
begin
  hstr := UPPERCASE(ini.readstring(
      sect, 'RootKey', 'HKEY_CURRENT_USER'));

You need to map this string into a Hive Constant; a direct string-to-cardinal conversion only works on string representaions of cardinal numbers, such as '123456789'.

Best way to do this is with a series of constants. You COULD define some sort of fancy mapping between a string and a DWORD, but this is just as straightforward.

  if hstr = 'HKEY_CLASSES_ROOT' then Reg.CurrentKey := HKEY_CLASSES_ROOT
  else
  if hstr = 'HKEY_CURRENT_USER' then Reg.CurrentKey := HKEY_CURRENT_USER
  else
  if hstr = 'HKEY_LOCAL_MACHINE' then Reg.CurrentKey := HKEY_LOCAL_MACHINE
  else
  if hstr = 'HKEY_USERS' then Reg.CurrentKey := HKEY_USERS
  else
  if hstr = 'HKEY_PERFORMANCE_DATA' then Reg.CurrentKey := HKEY_PERFORMANCE_DATA
  else
  if hstr = 'HKEY_CURRENT_CONFIG' then Reg.CurrentKey := HKEY_CURRENT_CONFIG
  else
  if hstr = 'HKEY_DYN_DATA' then Reg.CurrentKey := HKEY_DYN_DATA
  else
    ShowMsg('Hive not found: '+hstr);


OK. Now that we're in the right area of the registry, we have to go to the right key:

    Key := ini.readstring(sect, 'Path')
        +'\'
        + ini.readstring(sect, 'Key');
    if not Reg.KeyExists(Key) then
        ShowMessage('Key does not exist: '+Key)
    else
      etc.

As far as the typing goes, I think you'll need a similar chain of if's to the Hive:

    type := UPPERCASE(ini.readstring(sect, 'Type'));
    if type = 'BOOLEAN' then
    begin
        bool := ini.ReadBoolean(sect,'Data');
        RegType = REG_DWORD
    end

    else
    if type = 'STRING' then
    begin
        str :=  ini.ReadString(sect,'Data');
        RegType = REG_SZ
    end

    else
    .
    .
    .

and so on.

Hope this helped, and wasn't TOO obscure :)
0
 
LVL 33

Expert Comment

by:Slick812
ID: 7152606
hello hellfire, in your  strtocard(ini.readstring(Header, 'Type', 'HKEY_CURRENT_USER));

I'm not sure what your strtocard function does? ? ?
but if it uses char number values like '1234' then sending it a char value like 'HKEY_CURRENT_USER' won't work. Also the value you have  for the Ident is 'Type' and not 'Rootkey'

since HKEY_CURRENT_USER := DWORD($80000001); you might use a IntToStr(HKEY_CURRENT_USER) when writing to the RootKey of the inifile.

The .ini files are just charaters, the TIni gives you some WriteBoolean( ) but it just convets these to charaters, it seems like you would have to check the Reg type and then use '0' and '1' for boolean and IntToStr and StrToInt for Integer values
0
 

Author Comment

by:hellfire052497
ID: 7154773
Exactly what I needed, Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now