Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ini to registry

Posted on 2002-07-14
5
Medium Priority
?
495 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 800 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 34

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

885 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