Solved

Reading Registry

Posted on 1998-08-07
18
984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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