Solved

Writing, Removing and Searching Items in Windows Registery

Posted on 2004-04-05
9
350 Views
Last Modified: 2011-10-03
How Can I Do that (Question Title) ?
0
Comment
Question by:khalil_moghaddam
  • 5
  • 2
  • 2
9 Comments
 
LVL 7

Assisted Solution

by:sftweng
sftweng earned 20 total points
ID: 10763419
Have a look at the Delphi "TRegistry" class:

TRegistry is a low-level wrapper for the system registry and functions that operate on the registry.

Unit

Registry

Description

Use TRegistry to encapsulate access to the Windows system registry in an application. The registry is a database that an application can use to store and retrieve configuration information. Configuration information is stored in a hierarchical tree. Each node in the tree is called a key. Every key can contain subkeys and data values that represent part of the configuration information for an application.

All keys that an application creates, opens, reads, or writes are subkeys of predefined root keys. By default, a TRegistry object is created with a root key of HKEY_CURRENT_USER.

Only one key is accessible at a time in a TRegistry object. To determine the key that is currently accessible, read the value of the CurrentKey property. TRegistry methods enable an application to open, close, save, move, copy, and delete keys.

One or more data values containing actual configuration information can be stored in a key. TRegistry methods enable an application to query a key to see if it contains data, to read data in a key, and to write data to a key.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 80 total points
ID: 10765272
Create a new unit and call it 'UntReg'

unit UntReg;

interface

uses Windows, Registry, Classes, SysUtils;

procedure ReadRegistry;
procedure WriteRegistry;

var
 UserName: String;

implementation


// to write to the registry

procedure WriteRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Company Name\Application Name\', True) then
    begin
      Reg.WriteString('User Name', UserName);
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

// to read from the registry

procedure ReadRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\<Company Name Here>\<Applicaion Name Here>\', True) then
    begin
     if Reg.ValueExists('USerName') then
      UserName := Reg.ReadString('UserName');
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

initialization
  begin
    ReadRegistry;
  end;

finalization
begin
  WriteRegistry;
end;

end.


Now from your main form (unit)

from the menu choose

File --> Use Unit

Choose Unit2



Note: There are other methods of the TRegistry object as well (such as ReadInteger, ReadBool, etc)
Look at the TRegistry Object in your help file for all of the available methods

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765287
To search, you would have to know the key first , then test the key for the existence of a particular

first you open the key

 if Reg.OpenKey('\Software\<Company Name Here>\<Applicaion Name Here>\', True) then

then test for the existence

 if Reg.ValueExists('USerName') then
      UserName := Reg.ReadString('UserName');


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765293
To delete,

use the

 Reg.DeleteKey & Reg.DeleteValue methods


Shane
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:sftweng
ID: 10765658
Quite a comprehensive answer for a 20-point question! :-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765672
Cut & Paste (Isn't it great?)

Shane
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10766424

   Don't hardcode you strings like that ...

 create a unit with constants :

  unit MyAppConsts;
  const
     RegistryPath = '\Software\MyCompany\MyApplication\MyValue';
  ....

  then you need to defined the routine for read / write. Some of the guy above already did this, I would like to introduce some improvements - ability to read variants :

   uses MyAppConsts;
   ....
   function ReadRegistryValue (Name : String; ResType : TVarData) : Variant;
   var
      Reg : TRegistry;
   begin
      // open the registry
      Reg := TRegistry.Create;

      // open the key
      if Reg.OpenKey(RegistryPath, False) then
          begin
            case ResType of
              varInteger : Result := Reg.ReadInteger(Name);
              varString   : Result := Reg.ReadString(Name);
              varBoolean : Result := Reg.ReadBool(Name);
             // etc. there are other types, but these are enough
            end;
          end;

      // free the memory
      Reg.Free;
   end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10766482
<SMILE>

 I guess there was someone just as desperate for the 20 points

 <LOL>

Shane
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10772536

   not exactly ...

   The improvements :

   1) less lines of code
   2) more flexibility
   3) more useful

   <:P>
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now