fh1
asked on
Reading Registry
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?
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\So me_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.
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\So
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.
ASKER
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.
Doesn't the sample works?
ASKER
To ZifNab,
I can't make it to work.
I can't make it to work.
fh1, which problems do you get?
ASKER
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.
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.
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.
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.
ASKER
To Zif,
The result of nRet is somthing else( It is 2, not ERROR_SUCCESS)
The result of nRet is somthing else( It is 2, not ERROR_SUCCESS)
fh1,
and what result do you get if you use RegOpenKey.
By the way : only running sample I gave you... same Problems?
Zif.
and what result do you get if you use RegOpenKey.
By the way : only running sample I gave you... same Problems?
Zif.
ASKER
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.
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.
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
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
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.
ASKER
I can't read any value other than default value
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.
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.
ASKER
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.
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.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.