Board index » delphi » Accessing the Windows 95 Registry

Accessing the Windows 95 Registry

Hi all,

Wonder if someone here could just tell me how to access the Windows 95
Registry under Delphi 2.0 ?

I'm trying to access :

HKEY_LOCAL_MACHINE/system/currentcontrolset/services/vxd/mstcp

The variable is the HOSTNAME variable.

Any help you could give me would be greatly appreciated!

My email is matt...@flexnet.net

Regards,
Matthew.

 

Re:Accessing the Windows 95 Registry


Hi,
did you try the API functions whose names start with "Reg..."?
There are many of them i. e. "RegCreateKey" or "RegSetValue".
Perhaps you should have a look on one of these commands in your
help file. This will lead you to the whole group of commands.

There is also an object named "TRegistry" which is in the unit
"Registry". But there seems to be no documentation how to use
this object.

Does anyone know how to use it?

------------------------------------------------------------------
Dipl.-Ing. Ulrik Schoth, Lehrstuhl fuer Elektronische Schaltungen
Ruhr-Universitaet Bochum, ICFW 03/461, 44780 Bochum, Germany
ulrik.sch...@lems.ruhr-uni-bochum.de
ulrik.sch...@rz.ruhr-uni-bochum.de

Re:Accessing the Windows 95 Registry


H|!

Quote
>Wonder if someone here could just tell me how to access the Windows 95
>Registry under Delphi 2.0 ?
>I'm trying to access :
>HKEY_LOCAL_MACHINE/system/currentcontrolset/services/vxd/mstcp
>The variable is the HOSTNAME variable.

I'd like to know out the same thing!

                                 SBR.
Bu...@stk.ksu.ras.ru
{Excuse me please for my bad russian, my native language is Borland Pascal 8.3}

Re:Accessing the Windows 95 Registry


Quote
> >Wonder if someone here could just tell me how to access the Windows 95
> >Registry under Delphi 2.0 ?
> >I'm trying to access :
> >HKEY_LOCAL_MACHINE/system/currentcontrolset/services/vxd/mstcp
> >The variable is the HOSTNAME variable.

procedure DoIt;
var
  MyReg: TRegistry;
begin
  MyReg        := TRegistry.Create;
  MyReg.RootKey:= HKEY_LOCAL_MACHINE;
  if not MyReg.OpenKey( 'system\currentcontrolset\services\vxd\mstcp', false ) then begin
    raise Exception.Create( 'Trouble!' );
  end;
  ShowMessage( Format( 'Hostname: %s', [ MyReg.ReadString( 'HOSTNAME' ) ] ) );
  MyReg.CloseKey;
  MyReg.Free;
end;

You need to add Registry to your uses clause.
BTW: Don't forget to change forward slashes (/) into back slashes (\) !!!

As an added bonus, here goes a printable component complete with source,
plus a non visual component that uses TRegistry. I called it TQRProgramLabel
because it's a TQRLabel descendant that shows program related information from the
registry. I use it to have a template form from wich I derive the other
more specific reports, that fetch the company name's, address, phone #,
etc from the registry. To try it out, place a TProgramInfo and a
TQRProgramLabel on a report form; set the following properties for
ProgramInfo1: Company='Microsoft', Product='Windows',
Version='CurrentVersion'; set the following properties for
QRProgramLabel1: ProgramInfo='ProgramInfo1', RegKey='VersionNumber'.
That's all you need to see the version number of your windows 95
(usually 4.00.950).

Have Fun,
   Madruga

//*********************************************************************
unit MadCtls;

interface

uses
  Windows, Graphics, Classes, Controls, StdCtrls, DsgnIntf, Messages,
  Quickrep, Registry, SysUtils;

const
  cInvalidRegKey   = 'Invalid Registry Key!';
  cMissingRegKey   = 'Missing Registry Key!';
  cMissingProgInfo = 'Missing ProgInfo!';

type
  TProgramInfo = class( TComponent )
  private
    FCompany: string;
    FProduct: string;
    FVersion: string;
    FGlobal : TRegistry;
    FUser   : TRegistry;
    procedure RecreateRegs;
    procedure SetCompany( const aCompany: string );
    procedure SetProduct( const aProduct: string );
    procedure SetVersion( const aVersion: string );
    procedure SetUser   (       Path    : string;
                          const aValue  : string );
    procedure SetGlobal (       Path    : string;
                          const aValue  : string );
    function  GetUser   (       Path    : string ): string;
    function  GetGlobal (       Path    : string ): string;
  protected
    procedure Loaded; override;
  public
    constructor Create( aOwner: TComponent ); override;
    destructor  Done;
    property    User  [Index: string]: string read GetUser   write SetUser;
    property    Global[Index: string]: string read GetGlobal write SetGlobal;
  published
    property Company: string read FCompany write SetCompany;
    property Product: string read FProduct write SetProduct;
    property Version: string read FVersion write SetVersion;
  end;

  TQRProgramLabel = class(TQRLabel)
    FRegKey : string;
    FProgram: TProgramInfo;
    procedure SetKey        ( New: string       );
    procedure SetProgramInfo( New: TProgramInfo );
  protected
    procedure MySetCaption; virtual;
    procedure Notification( AComponent: TComponent; Operation: TOperation ); override;
    procedure Print( X, Y: integer ); override;
  public
    constructor Create( aOwner: TComponent ); override;
  published
    property RegKey     : string       read FRegKey  write SetKey;
    property ProgramInfo: TProgramInfo read FProgram write SetProgramInfo;
  end;

procedure Register;

implementation

constructor TQRProgramLabel.Create( aOwner: TComponent );
begin
  inherited Create( aOwner );
  Caption := cMissingProgInfo;
end;

procedure TQRProgramLabel.SetKey( New: string );
begin
  FRegKey := New;
  MySetCaption;
end;

procedure TQRProgramLabel.SetProgramInfo( New: TProgramInfo );
begin
  FProgram := New;
  MySetCaption;
end;

procedure TQRProgramLabel.Notification( AComponent: TComponent; Operation: TOperation );
begin
  inherited Notification( AComponent, Operation );
  if (Operation = opRemove) and (AComponent = FProgram) then begin
    FProgram := nil;
    MySetCaption;
  end;
end;

procedure TQRProgramLabel.Print( X, Y: integer );
begin
  if (Caption='') then begin
    MySetCaption;
  end;
  inherited Print( X, Y );
end;

procedure TQRProgramLabel.MySetCaption;
var
  S: string;
begin
  if Assigned(FProgram) then begin
    if (FRegKey<>'') then begin
      S := FProgram.Global[FRegKey];
      if S='' then begin
        Caption := cInvalidRegKey;
      end else begin
        Caption := S;
      end;
    end else begin
      Caption := cMissingRegKey;
    end;
  end else begin
    Caption := cMissingProgInfo;
  end;
end;

constructor TProgramInfo.Create( aOwner: TComponent );
begin
  inherited Create( aOwner );
  FUser    := TRegistry.Create;
  FGlobal  := TRegistry.Create;
  FCompany := '';
  FProduct := '';
  FVersion := '';
end;

destructor TProgramInfo.Done;
begin
  FGlobal.Free;
  FUser  .Free;
  FGlobal := nil;
  FUser   := nil;
end;

procedure TProgramInfo.Loaded;
begin
  inherited Loaded;
  RecreateRegs;
end;

procedure TProgramInfo.RecreateRegs;
begin
  if FUser  .CurrentKey<>0 then FUser  .CloseKey;
  if FGlobal.CurrentKey<>0 then FGlobal.CloseKey;
  FGlobal.RootKey:= HKEY_LOCAL_MACHINE;
  FUser  .RootKey:= HKEY_CURRENT_USER;
  if (FCompany<>'') and (FProduct<>'') and (FVersion<>'') then begin
    if not FUser  .OpenKey( 'Software\'+FCompany+'\'+FProduct+'\'+FVersion, false ) then raise Exception.Create( 'Program not installed...' );
    if not FGlobal.OpenKey( 'SOFTWARE\'+FCompany+'\'+FProduct+'\'+FVersion, false ) then raise Exception.Create( 'Program not installed...' );
  end;
end;

procedure TProgramInfo.SetCompany( const aCompany: string );
begin
  FCompany := aCompany;
  RecreateRegs;
end;

procedure TProgramInfo.SetProduct( const aProduct: string );
begin
  FProduct := aProduct;
  RecreateRegs;
end;

procedure TProgramInfo.SetVersion( const aVersion: string );
begin
  FVersion := aVersion;
  RecreateRegs;
end;

procedure TProgramInfo.SetUser( Path: string; const aValue: string );
begin
  if Assigned( FUser ) and (FUser.CurrentKey<>0) then begin
    FUser.WriteString( Path, aValue );
  end;
end;

procedure TProgramInfo.SetGlobal( Path: string; const aValue: string );
begin
  if Assigned( FGlobal ) and (FGlobal.CurrentKey<>0) then begin
    FGlobal.WriteString( Path, aValue );
  end;
end;

function TProgramInfo.GetUser( Path: string ): string;
begin
  if Assigned( FUser ) and (FUser.CurrentKey<>0) then begin
    Result := FUser.ReadString( Path );
  end else begin
    Result := '';
  end;
end;

function TProgramInfo.GetGlobal( Path: string ): string;
begin
  if Assigned( FGlobal ) and (FGlobal.CurrentKey<>0) then begin
    Result := FGlobal.ReadString( Path );
  end else begin
    Result := '';
  end;
end;

procedure Register;
begin
  RegisterComponents( 'Mad', [TProgramInfo,
                              TQRProgramLabel] );
end;

end.
{$WARNINGS OFF}
 Copyright ?1996, Fernando M. B. MADruga
  . . . . . . . . . . . . . . . . . . .
  . # . . . # . . # # # . . # # # . . .
  . # # . # # . # . . . # . # . . # . .
  . # . # . # . # . . . # . # . . . # .
  . # . . . # . # # # # # . # . . . # .
  . # . . . # . # . . . # . # . . # . .
  . # . . . # . # . . . # . # # # . . .
  . . . . . . . . . . . . . . . . . . .
//********************************************************

Re:Accessing the Windows 95 Registry


Quote
Ulrik Schoth wrote:

> Hi,
> did you try the API functions whose names start with "Reg..."?
> There are many of them i. e. "RegCreateKey" or "RegSetValue".
> Perhaps you should have a look on one of these commands in your
> help file. This will lead you to the whole group of commands.

> There is also an object named "TRegistry" which is in the unit
> "Registry". But there seems to be no documentation how to use
> this object.

> Does anyone know how to use it?

> ------------------------------------------------------------------
> Dipl.-Ing. Ulrik Schoth, Lehrstuhl fuer Elektronische Schaltungen
> Ruhr-Universitaet Bochum, ICFW 03/461, 44780 Bochum, Germany
> ulrik.sch...@lems.ruhr-uni-bochum.de
> ulrik.sch...@rz.ruhr-uni-bochum.de

if you look up TRegIniFile you will see an object which works with the registry
in the same way as the TIniFile works for .INI files.  Seems to work fine.

brian

Re:Accessing the Windows 95 Registry


H| all!

 I have already send the solution to this newsgroup, but I suppose that it
was killed by my news-server. :) So, I'm sending it again:

There is a routine in the Windows.DCU called RegSetValueEx.
Syntax:
  function RegSetValueEx(hKey: HKEY; lpValueName: PChar; Reserved: DWORD;
           dwType: DWORD; lpData: Pointer; cbData: DWORD): Longint; stdcall;

Example:

procedure TForm1.Button1Click(Sender: TObject);
var
 k1:hkey;
 k2,k3:hkey;
 l:longint;
 p:pchar;
begin
 l:=regopenkey(HKEY_LOCAL_MACHINE,'SOFTWARE',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('HKEY '+inttostr(l));
  end;

 l:=regopenkey(k1,'Microsoft',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('MS '+inttostr(l));
  end;

 l:=regopenkey(k1,'Windows',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('WIN '+inttostr(l));
  end;

 l:=regopenkey(k1,'CurrentVersion',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('CURVER '+inttostr(l));
  end;

 l:=regopenkey(k1,'Uninstall',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('UNI '+inttostr(l));
  end;

 l:=regcreatekey(k1,'BIB Business 1.0',k2);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('BIB '+inttostr(l));
  end;

 l:=regopenkey(k1,'BIB Business 1.0',k1);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('BR '+inttostr(l));
  end;

 p:='BIB Business 1';
 l:=regsetvalueEx(k1,'DisplayName',0,1,p,28);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('DN '+inttostr(l));
  end;

 p:='d:\langs\programs\test.uni';
 l:=regsetvalueEx(k1,'UninstallString',0,1,p,44);
 if l<>ERROR_SUCCESS then
  begin
   showmessage('UNI '+inttostr(l));
  end;

 showmessage('Finished!');
end;

Best wishes!

                                        SBR.
Bu...@stk.ksu.ras.ru
{Excuse me please for my bad russian, my native language is Borland Pascal 8.3}

Re:Accessing the Windows 95 Registry


Quote
Ulrik Schoth <ulrik.sch...@rz.ruhr-uni-bochum.de> writes:
>Hi,
>did you try the API functions whose names start with "Reg..."?
>There are many of them i. e. "RegCreateKey" or "RegSetValue".
>Perhaps you should have a look on one of these commands in your
>help file. This will lead you to the whole group of commands.
>There is also an object named "TRegistry" which is in the unit
>"Registry". But there seems to be no documentation how to use
>this object.
>Does anyone know how to use it?

I am using all this stuff, and after few weeks of troubles finally got
everything working. The problem is as usual - connecting C and Pascal
calls - all dynamic variables in pascal MUST be initialized before calling
API functions, it sometimes looks weird as API often returns null-reminated
strings, whose size is unknown beforehand, so you should allocate more memory.

TRegistry is very primitive component, so I ended up writing
few procedure and functions which retreive necessary keys from
the registry. Other users may find TRegistry helpful, but I'm storing a
very complicated information in it and this component doesnt work for me.

Interesting, that in Beta Delphi 2.0 release there were references to Windows
API functions, but they vanished in the updated HLP file which I got from
the Borland site. However, if you travel down the directory tree in the
Delphi directory, you will find the Windows.pas file which has at least
Pascal interface to API, so you will have basic idea about parameters.

Hope this will help.

Greg Zevin

Re:Accessing the Windows 95 Registry


Quote
Ulrik Schoth <ulrik.sch...@rz.ruhr-uni-bochum.de> wrote:
>There is also an object named "TRegistry" which is in the unit
>"Registry". But there seems to be no documentation how to use
>this object.  Does anyone know how to use it?

The only guidance so far is an entry in the online help for
TRegIniFile, which says:

"The TRegIniFile object is an indirect descendant of TObject. It has
all the same methods and properties as the TIniFile object. The only
difference is that instead of processing an .INI file, TRegIniFile
reads from and writes to the system registry, using the FileName
property as a subkey under the HKEY_CURRENT_USER key. Refer to the
printed documentation and VCL help file for information on TIniFile's
methods and properties."

Check thru the demo files.  I think there is one which uses the
registry.

Ted Lassagne
Napa, CA

Re:Accessing the Windows 95 Registry


RH>Subject: Re: Delphi 2.0 ......Worth upgrading ?
RH>Date: Sat, 20 Apr 1996 23:22:56 GMT

Quote
RH>Mark Magers <Mm...@ix.netcom.com> wrote:

RH>>I read a lot of negitive posts in this newsgroup about Delphi 2.0, does
RH>>anyone have anything positive to say about this product?, I've been
RH>developing

RH>>with Delphi 1.0 for a year now and have no major complaints.
RH>>Any feedback on your OVERALL impression of Delphi 2.0 would be very muc
RH>h
RH>>appreciated.

RH>>Thanks,
RH>>Mark..Mm...@x.netcom.com

RH>Delphi 2.0 is great. It is best best development tool I can imagine.
RH>It is fast, has a really great functionality and supports all new
RH>Microsoft specifications. You can do everything with it.

RH>The only problem: it doesn't work.

  Correct, and it has no idea what to do with OCX's written to
MS standards. :(

  Hal

Other Threads