Unit
Windows.PasSyntax
RegOpenKeyEx(
hKey: HKEY; {handle of the parent key to open}
lpSubKey: PChar; {the sub key name}
ulOptions: DWORD; {reserved}
samDesired: REGSAM; {key security requested}
var phkResult: HKEY {handle of new open key}
): Longint; {returns an error code}Description
This function is used to open an existing sub key in the registry or to open another handle to an open key.Parameters
hKey: This can be any open key or one of the following root key values.HKEY_CLASSES_ROOTHKEY_CURRENT_USERHKEY_LOCAL_MACHINEHKEY_USERSlpSubKey: A pointer to a null terminated string that contains the name of the sub key to open. If this parameter points to an empty string, the function will open another handle to the key identified by the hKey parameter.ulOptions: Reserved for future use. Set this parameter to zero.samDesired: Specifies the security access option requested.phkResult: A variable that will receive the new handle to the opened key.Return Value
If the function succeeds, it returns ERROR_SUCCESS. If the function fails, it returns an error code that can be used with the FormatMessage function to retrieve an error message.The Tomes of Delphi 3: Win32 Core API Help File by Larry DiehlE:
function OpenNodeKey(sNode: TTreeNode; var KeyName: string): hKey;
var
  iNode: TTreeNode;             // a tree node pointer
  RootKey: hKey;                // a handle to the root key of the specific node
begin
  {initialize the temporary pointer to the specified treenode}
  iNode := sNode;  {initialize the root key handle}
  RootKey := 0;  {begin working our way up the tree until we hit the root,   building a comprehensive registry key name from the treenode strings.}
  if sNode.Level > 1 then
  begin
    KeyName := sNode.Text;
    iNode := sNode.Parent;
    while iNode.Level > 1 do
    begin
      KeyName := iNode.Text+'\'+KeyName;
      iNode := iNode.Parent;
    end;
  end;  {retrieve the root key for the specified node}
  case iNode.Index of    0: RootKey := HKEY_CLASSES_ROOT;
    1: RootKey := HKEY_CURRENT_USER;
    2: RootKey := HKEY_LOCAL_MACHINE;
    3: RootKey := HKEY_USERS;
    4: RootKey := HKEY_PERFORMANCE_DATA;
    5: RootKey := HKEY_CURRENT_CONFIG;
    6: RootKey := HKEY_DYN_DATA;
  end;The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl

解决方案 »

  1.   

    Unit
    Windows.PasSyntax
    RegCloseKey(
    hKey: HKEY {handle of the key to close}
    ): Longint; {returns an error code}Description
    This function releases the open registry key handle retrieved from a prior call to the RegCreateKeyEx or RegOpenKeyEx functions. Calling RegClose key does not guarantee that a new value is written to the registry. The system can take up to a several seconds to write the new values. To guarantee that all values are written to the registry, call the RegFlushKey function.Parameters
    hKey: Specifies the handle of the registry key to close.Return Value
    If the function succeeds, it returns ERROR_SUCCESS. If the function fails, it returns an error code that can be used with the FormatMessage function to retrieve an error message.The Tomes of Delphi 3: Win32 Core API Help File by Larry DiehlE:
    procedure TForm1.Key1Click(Sender: TObject);
    var
      KeyName: string;          // holds a registry key name
      NewName: string;          // holds the new registry key name
      nKey,                     // holds handles the opened and new keys
      pKey: hKey;
      KeyResult: DWORD;         // holds the disposition value
    begin
      {retrieve the name for the new key}
      NewName := InputBox('Enter new Key ','Enter New Key Name','');  {if a name was specified, open the parent key from the selected treenode}
      pKey:=0;
      if NewName <> '' then
        pKey := OpenNodeKey(TreeView1.Selected, KeyName);  {if the parent key was opened...}
      if pKey = 0 then
        ShowMessage('Error opening parent key')
      else
      begin
        {...create a new child key with the specified name}
        RegCreateKeyEx(pKey, PChar(NewName), 0, nil, 0, Key_All_Access,                   nil, nKey, @KeyResult);    {if the key did not already exist, add it to the treeview}
        if KeyResult = REG_OPENED_EXISTING_KEY then
          ShowMessage('Key Already Exists')
        else
        begin
          TreeView1.Selected:=TreeView1.Items.AddChild(TreeView1.Selected,NewName);
          TreeView1.Selected.ImageIndex := 0;
          TreeView1.Selected.SelectedIndex := 1;
        end;    {close both the parent key and the child key}    RegCloseKey(nKey);
        RegCloseKey(pKey);
      end;
    end;The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
      

  2.   

    Unit
    Windows.PasSyntax
    RegDeleteKey(
    hKey: HKEY; {handle to the parent key of the sub key to delete}
    lpSubKey: PChar {specifies the name of the key to delete}
    ): Longint; {returns an error code}Description
    This function is used to delete a sub key from inside of a parent key. The function deletes the sub key and all of its associated values from the registry.ParametershKey: Specifies the handle of the parent key of the sub key to delete. This can be any open key or one of the following root key values.HKEY_CLASSES_ROOTHKEY_CURRENT_USERHKEY_LOCAL_MACHINEHKEY_USERSlpSubKey: A pointer to a null terminated string that contains the name of the sub key to delete.Return Value
    If the function succeeds, it returns ERROR_SUCCESS. If the function fails, it returns an error code that can be used with the FormatMessage function to retrieve an error message.The Tomes of Delphi 3: Win32 Core API Help File by Larry DiehlE:
    procedure TForm1.DeleteKey2Click(Sender: TObject);
    var
      KeyName: string;                    // holds the name of the key to be deleted
      pValueName: array[0..100] of char;  // holds the value name
      pKey: hKey;                         // the handle of the key to be deleted
    begin
      {insure that the user has not elected to delete a base key}
      if TreeView1.Selected.Level <= 1 then    ShowMessage('Cannot Delete base Keys')
      else
        {confirm that the selected key is to be deleted}
        if MessageBox(Form1.Handle, 'Are you sure you want to delete this registry'+
                      ' key?', 'Confirmation Box', MB_OKCANCEL) = IDOK then
        begin
          {open the selected key's parent}
          pKey := OpenNodeKey(TreeView1.Selected.Parent, KeyName);      if pKey = 0 then        ShowMessage('Cannot open parent key')
          else
          begin
            StrPCopy(pValueName, TreeView1.Selected.Text);
            {if the parent key was opened, delete the selected key}
            if RegDeleteKey(pKey, pValueName)= 0 then
              TreeView1.Items.Delete(TreeView1.Selected)
            else
              ShowMessage('Could not delete key');
          end;
        end;
    end;The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
      

  3.   

    Delphi已封装了注册表的有关函数,大大简化了操作,你为什么还要用API呢?
      

  4.   

    uses Registry;
    ..........
    var 
     Reg:TRegIniFile;
     S:String;
    ..........
    Reg:=TRegIniFile.Create;
    Reg.WriteString('Key','Title','Value');
    S:=Reg.ReadString('Key','Title','');TRegIniFile的其他方法都与此非常类似,可以照此模仿呀!
    说实在的,我觉得这样操作已经很方便了,为什么偏要用 API 呢???
      

  5.   

    糟了,补充一下,楼上WriteString,ReadString前,要:Reg.RootKey:=HKEY_CURRENT_USER;  或其他项名!