Board index » delphi » DELPHI 3.0 - Trouble figuring out registry tools provided by the Windows API - All I want is to read a string from a registry key!

DELPHI 3.0 - Trouble figuring out registry tools provided by the Windows API - All I want is to read a string from a registry key!

Quote
In article <3412a379.107903...@news.netwrx.net> lsav...@netwrx.net writes:
>My whole goal is to extract the computer name of the Win95 machine
>through a Delphi function.  I noticed that the computer name is stored
>in the registry, but I am having problems accessing the registry with
>the Windows API functions proivded; and I have never attempted
>anything like this.
>I'm trying to access the string COMPUTERNAME from the registry key :
>System\CurrentControlSet\control\ComputerName\ComputerName
>under the HIVE :
>HKEY_LOCAL_MACHINE.
>I'm trying to use the Windows API function, RegQueryValue but it wont
>compile because it doesn't like the last parameter which the function
>(In Windows API help) returns as the length of the buffer string.  The
>type it requires is PLONG which I assume is a pointer, but Delphi
>isn't familiar with the type PLong.  I tried using a generic pointer
>but it still doesn't like ir.  Does anyone have a code snippet of a
>quick-and-dirty way to access a string under a key in the windows 95
>registry?

I don't have source code I can send you, because my solution to the problem
was to buy SysTools.  Looking at their code, however, I can see at a glance
that I would not have had a clue how to write it and would have spent many
weeks (that I did not have) trying to replicate what it does.
 

Re:DELPHI 3.0 - Trouble figuring out registry tools provided by the Windows API - All I want is to read a string from a registry key!


My whole goal is to extract the computer name of the Win95 machine
through a Delphi function.  I noticed that the computer name is stored
in the registry, but I am having problems accessing the registry with
the Windows API functions proivded; and I have never attempted
anything like this.

I'm trying to access the string COMPUTERNAME from the registry key :

System\CurrentControlSet\control\ComputerName\ComputerName

under the HIVE :

HKEY_LOCAL_MACHINE.

I'm trying to use the Windows API function, RegQueryValue but it wont
compile because it doesn't like the last parameter which the function
(In Windows API help) returns as the length of the buffer string.  The
type it requires is PLONG which I assume is a pointer, but Delphi
isn't familiar with the type PLong.  I tried using a generic pointer
but it still doesn't like ir.  Does anyone have a code snippet of a
quick-and-dirty way to access a string under a key in the windows 95
registry?

Thanks!

        -Lee

Re:DELPHI 3.0 - Trouble figuring out registry tools provided by the Windows API - All I want is to read a string from a registry key!


Quote
In article <3412a379.107903...@news.netwrx.net>, lsav...@netwrx.net wrote:
>My whole goal is to extract the computer name of the Win95 machine
>through a Delphi function.  I noticed that the computer name is stored
>in the registry, but I am having problems accessing the registry with
>the Windows API functions proivded; and I have never attempted
>anything like this.

>I'm trying to access the string COMPUTERNAME from the registry key :

>System\CurrentControlSet\control\ComputerName\ComputerName

>under the HIVE :

>HKEY_LOCAL_MACHINE.

>I'm trying to use the Windows API function, RegQueryValue but it wont
>compile because it doesn't like the last parameter which the function
>(In Windows API help) returns as the length of the buffer string.  The
>type it requires is PLONG which I assume is a pointer, but Delphi
>isn't familiar with the type PLong.  I tried using a generic pointer
>but it still doesn't like ir.  Does anyone have a code snippet of a
>quick-and-dirty way to access a string under a key in the windows 95
>registry?

>Thanks!

>        -Lee

Here you go...
But if you're looking for the computername, then check out the code after the
registry coding...

Function ExtractKeyValue(RegVal : STRING):STRING;
Begin
 ExtractKeyValue:=Copy(RegVal,Pos('=',RegVal)+1,Length(RegVal)-Pos('=',
RegVal));
End;

function GetValue(FKey,AValue:String; FRootKey  : LONGINT): Variant;
var    Registry    : TRegistry;
        AValueData  : Variant;
begin
  Result:='';
  if FKey <> '' then begin
    If FKey[Length(FKey)]='\' then Delete(FKey,Length(FKey),1);
    Registry := TRegistry.Create;
    try
     Registry.RootKey := FRootKey;//Like predefined HKEY_CURRENT_USER
     if Registry.OpenKey(FKey,False) then
      begin
       case Registry.GetDataType(AValue) of
        rdString: AValueData :=
Registry.ReadString({ExtractKeyValue(AValue)}AValue);
        rdInteger: AValueData :=
Registry.ReadInteger({ExtractKeyValue}(AValue));
        //rdBinary: ShowMessage('rdBinary');
        //rdExpandString: ShowMessage('rdExpandString');
        //rdUnknown: ShowMessage('rdUnknown');
       else
        //ShowMessage('Could not find data....');
       end;
       result := AValueData;
      end;
   finally
      Registry.Free;
    end;
   end;
end;

Usage:  
MyVar:=GetValue('System\CurrentControlSet\control\ComputerName','ComputerName',
HKEY_LOCAL_MACHINE);

Computername...

Function GetName:STRING;
VAR       CName : ARRAY [0..79] of Char;
          Buffer     : DWORD;
begin
  Buffer:=79;
  If GetComputerName(@CName,Buffer) then Result:=CName
  Else Result:='';
End;

Hope this helps you!

I haven't tested the code though, but I think it should work pretty good...

/Timmy...

PS. Don't forget to get rid of the section containg spam fooling info.

---------------------------------------------------------
Windows NT 5, codename: BlueScreen
---------------------------------------------------------

Re:DELPHI 3.0 - Trouble figuring out registry tools provided by the Windows API - All I want is to read a string from a registry key!


Quote
Timmy Lind wrote:
> In article <3412a379.107903...@news.netwrx.net>, lsav...@netwrx.net
> wrote:
> >...
> >I'm trying to use the Windows API function, RegQueryValue but it wont

> >compile because it doesn't like the last parameter which the function

> >(In Windows API help) returns as the length of the buffer string.
> The
> >type it requires is PLONG which I assume is a pointer, but Delphi
> >isn't familiar with the type PLong.  I tried using a generic pointer
> >but it still doesn't like ir.  Does anyone have a code snippet of a
> >quick-and-dirty way to access a string under a key in the windows 95
> >registry?

pointers to variables as api-function-parameters are usually replaced
with var-parameters within the interface unit, so you should simply try
to pass a normal integer, longint or dword variable to the function. the
declaration as var does all the referencing and dereferencing by itself
(i haven't seen such a genial feature in C :^)).

--asem

Other Threads