Solved

Reading Registry

Posted on 1998-08-07
18
983 Views
Last Modified: 2008-02-01
I am trying to read a value of a subkey from registry in delphi 1.0. The API function RegQueryValue can only read a default value. How can I read the data of a value other than the default value?
0
Comment
Question by:fh1
  • 7
  • 7
  • 2
  • +2
18 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361666
You can use these methods :

function RegOpenKey(Key: HKey; SubKey: PChar; var Result: HKey):
                             Longint;
function RegCreateKey(Key: HKey; SubKey: PChar;var Result: HKey):
                             Longint;
function RegCloseKey(Key: HKey): Longint;
function RegDeleteKey(Key: HKey; SubKey: PChar): Longint;
function RegSetValue(Key: HKey; SubKey: PChar; ValType: Longint;
                     Value: PChar; cb: Longint): Longint;
function RegQueryValue(Key: HKey; SubKey: PChar; Value: PChar;
                        var cb: Longint): Longint;
function RegEnumKey(Key: HKey; index: Longint; Buffer: PChar;
                        cb: Longint): Longint;

Look out :

the Win16 registry does not allow more than one value under a key, you have to create a subkey for each value you want to store. So, you are only able to assign one single value to the (Standard/Default) identifier.

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361667
Here is sample code I found somewhere on my disk... didn't knew I had this... :-) :

Registry Example Source Code (16 and 32-bit)


Here is an example of registry management code I wrote to play with
registry management..

unit Reg;

interface

Uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Tabs, TypeDefs;

Var
   bSaveWindowPos         :  Boolean;
   bUsePassword           :  Boolean;

Const
   RootKey                = 'Software\Some_Software\Some_Product';

function  BuildRegistryTree( hKey : Longint; var aList : TStringList; szKey :
PChar ) : Boolean;
procedure ChangeRegistryKey( aKey : String; aValue : String );
procedure GetRegistryKey( aKey : String; var aValue : String; Default :
String );
function  GetRegistryInt( aKey : String; Default : Integer ) : Integer;


implementation

procedure ChangeRegistryKey( aKey : String; aValue : String );
Var
   szKey, szValue   : PStr;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( Length( aValue ) + 1 );
  StrPCopy( szKey, aKey );
  StrPCopy( szValue, aValue );
  RegSetValue( HKEY_CLASSES_ROOT, szKey, REG_SZ, szValue, StrLen( szValue ));
  StrDispose( szKey );
  StrDispose( szValue );
end;

procedure GetRegistryKey( aKey : String; var aValue : String; Default :
String );
Var
   szKey, szValue   : PStr;
   nRet, nSize      : LongInt;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( 1000 );
  StrPCopy( szKey, aKey );
  StrPCopy( szValue, aValue );
  nSize := 1000;
  nRet := RegQueryValue( HKEY_CLASSES_ROOT, szKey, szValue, nSize );
  if (nRet = ERROR_SUCCESS) then
     aValue := StrPas( szValue )
    else
     aValue := Default;
  StrDispose( szKey );
  StrDispose( szValue );
end;

function GetRegistryInt( aKey : String; Default : Integer ) : Integer;
Var
   szKey, szValue   : PStr;
   aString          : String;
   nRet, nSize      : LongInt;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( 32 );
  StrPCopy( szKey, aKey );
  nSize := 32;
  nRet := RegQueryValue( HKEY_CLASSES_ROOT, szKey, szValue, nSize );
  aString := StrPas( szValue );
  StrDispose( szKey );
  StrDispose( szValue );
  if (nRet = ERROR_SUCCESS) then
     GetRegistryInt := StrToInt( aString )
    else
     GetRegistryInt := Default;
end;


function BuildRegistryTree( hKey : LongInt; var aList : TStringList; szKey :
PChar ) : Boolean;
var
  hRoot          : LongInt;
  lItem          : LongInt;
  hError         : LongInt;
  pData          : PChar;
  aString        : String;

begin
  hRoot := 0;
  lItem := 0;
  pData := StrAlloc( 1024 );

  hError := ERROR_SUCCESS;

  if (RegOpenKey( hKey, szKey, hRoot ) = ERROR_SUCCESS) then
  begin
     while (hError = ERROR_SUCCESS) do
     begin
        hError := RegEnumKey( hRoot, lItem, pData, 1024 );

        if (hError = ERROR_SUCCESS) then
        begin
           aList.Add( StrPas( pData ));
           Inc(lItem);
        end;

     end;
     RegCloseKey( hRoot );
  end;

  StrDispose( pData );
end;

var
   RootString : String;
   aString    : String;

begin
     RootString := RootKey + '\Defaults';

     {********************************************************************
     *
     *  Get the bSaveWindowPos value
     *
     ********************************************************************}

     GetRegistryKey( RootString + '\SaveWindowPos', aString, 'False' );
     if (UpperCase(aString) = 'TRUE' ) then
        bSaveWindowPos := True
       else
        bSaveWindowPos := False;

     {********************************************************************
     *
     *  Get the bSaveWindowPos value
     *
     ********************************************************************}

     GetRegistryKey( RootString + '\UseDefaultWidth', aString, 'False' );
     if (UpperCase(aString) = 'TRUE' ) then
        bUsePassword := True
       else
        bUsePassword := False;
end.

Randy Dryburgh
CodePig@aol.com
CDeveloper@gnn.com

Regards, Zif.
0
 

Author Comment

by:fh1
ID: 1361668
The problem is that I have to use delphi 1.0 for my project, but I have to access the registry of WindowsNT 4.0 or Windows 95.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1361669
Doesn't the sample works?
0
 

Author Comment

by:fh1
ID: 1361670
To ZifNab,
I can't make it to work.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361671
fh1, which problems do you get?
0
 

Author Comment

by:fh1
ID: 1361672
To ZifNab,
I am opening the key with RegOpenKey and trying to read the value of a subkey with GetRegistryKey, but I can't read any value other than default.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361673
fhl1,

In the following code, what is the result of nRet? ERROR_SUCCES? Or something else.

--
  nRet := RegQueryValue( HKEY_CLASSES_ROOT, szKey, szValue, nSize );
      if (nRet = ERROR_SUCCESS) then
         aValue := StrPas( szValue )
        else
         aValue := Default;

---

Zif.
0
 

Author Comment

by:fh1
ID: 1361674
To Zif,
The result of nRet is somthing else( It is 2, not ERROR_SUCCESS)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361675
fh1,

and what result do you get if you use RegOpenKey.

By the way : only running sample I gave you... same Problems?

Zif.
0
 

Author Comment

by:fh1
ID: 1361676
To Zif,
The result for RegOpenKey is 0 or ERROR_SUCCESS.
Running your sample has the same result, it can only read the default value and not the second value.
0
 
LVL 2

Expert Comment

by:333
ID: 1361677
Hi,
I found this in help:
"Function RegQueryValue retrieves the data for a key's first value that has a NULL name. This function is provided for compatibility with Windows version 3.1. Win32-based applications should use the RegQueryValueEx function."
Maybe there is the problem?

AP
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361678
fhl, I'm sorry that I can't help you. I don't know what 's wrong. Just reject my answer. I let you know when I find something new. Sorry.
0
 

Author Comment

by:fh1
ID: 1361679
I can't read any value other than default value
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361680
hi, fh1.

i've tested around a little bit.
normally, i use delphi 3 4 registry-access (naturally).
when testing the whole crap with delphi 1, i found that in ShellApi.pas (which the registry-functions r interfaced in), only contains
  RegOpenKey
  RegCreateKey
  RegCloseKey
  RegDeleteKey
  RegSetValue
  RegQueryValue
  RegEnumKey
and as the only rootkey
  HKEY_CLASSES_ROOT

these r the funtions declared in WinAPI 3.1.
problem with this is, in Windows 3.1x, the registry has exactly 1  value per key (always string).
in win95/nt, u can have several entries with one value each per key.

example (just 4 definition of terms):

win95/NT:
rootkey = HKEY_CLASSES_ROOT
key = cfile
entries =  
 (Standard)
 AlwaysShowExt
values =
 "C-Quelldatei"
 ""

- here u have 2 entries in 1 key

win3.1x always only has 1!
that's the reason why u only can get the 'default'-value -
the 3.1-functions r not capable of specifying a certain value within a key 2 query.

only chance 4 u is indeed 2 change 2 delphi 3, cos it is capable of using the above mentioned (by 333) extended registry-functions (regqueryex, regenumvalue etc.) 4 win32-based apps.

sorry, no f... way 4 u 2 get through with d1.

btw:
whatz the reason u insist in d1?

so long,

Black Death.



0
 

Author Comment

by:fh1
ID: 1361681
To Black Death,
As I said before:'The problem is that I have to use delphi 1.0 for my project, but I have to access the registry of WindowsNT 4.0 or Windows 95.' This is an old software we developed for our client in delphi 1.0 and we still maintaing it.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361682
hi fh1.

in this case, i can think of nothing else than implementing a little program in delphi3 which you call from within your application and which will read the required values to hand them over to your app. sorry, no other ideas.

so long,
Black Death.
0
 
LVL 1

Accepted Solution

by:
duke_n earned 50 total points
ID: 1361683
If you have a name of the value (string)then here are functions you need:
Tregistry.readstring
Tregistry.readfloat
Tregistry.readinteger
and so on...
Bye
Duke Nukem.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 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