Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

Ini to registry

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
hellfire052497
Asked:
hellfire052497
1 Solution
 
slaviknCommented:
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
 
slaviknCommented:
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
 
OryxConLaraCommented:
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
 
Slick812Commented:
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
 
hellfire052497Author Commented:
Exactly what I needed, Thanks.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now