Recent

Author Topic: [Code] Jump to a Key branch (or value) on the Windows Registry  (Read 2203 times)

u2o

  • Jr. Member
  • **
  • Posts: 72
  • No message
[Code] Jump to a Key branch (or value) on the Windows Registry
« on: January 05, 2014, 09:35:57 am »
I hope it will be helpful, such procedures are complicated and not very easy to understand.

unitregeditjump.pas
Code: [Select]
unit UnitRegEditJump;
// Last Edit 2013.10.12

interface

uses
  Windows,   {hwnd}
  shellapi,  {ShellExecuteInfo}
  Classes, SysUtils;

type
  TRegEditJump = class
  public
    class procedure JumpToKey(Key: string; Value: string = '');
  end;

implementation

class procedure TRegEditJump.JumpToKey(Key: string; Value: string = '');

{ procedure based on http://www.sharejs.com/codes/delphi/6722 }
{ procedure based on http://kvrsoft.com/articles/delphi-articles/13-system/2217-regedit }

var
  nIndex, nCount: integer;
  HWND_REGEDIT: HWND;
  HWND_ListView: HWND;
  HWND_TreeView: HWND;
  ExecInfo: ShellExecuteInfoA;
begin

  ExecInfo.hProcess := 0;                               { initialize }

  HWND_REGEDIT := FindWindowA(PChar('RegEdit_RegEdit'), nil);

  if HWND_REGEDIT = 0 then begin                        { If Regedit is not running, run }
    FillChar(ExecInfo, 60, #0);
    with ExecInfo do begin
      cbSize := 60;
      fMask := SEE_MASK_NOCLOSEPROCESS;
      lpVerb := PChar('open');
      lpFile := PChar('regedit.exe');
      nShow := 1;
    end;
    ShellExecuteExA(@ExecInfo);
    WaitForInputIdle(ExecInfo.hProcess, 200);
    HWND_REGEDIT := FindWindowA(PChar('RegEdit_RegEdit'), nil);
  end;

  ShowWindow(HWND_REGEDIT, SW_SHOWNORMAL);

  // * Set the focus on the [TreeView] ( Keys\Subkeys )
  HWND_TreeView := FindWindowExA(HWND_REGEDIT, 0, PChar('SysTreeView32'), nil);
  SetForegroundWindow(HWND_TreeView);

  // - - - Goto the principal branch
  nIndex := 30;
  repeat
    SendMessageA(HWND_TreeView, WM_KEYDOWN, VK_LEFT, 0);
    Dec(nIndex);
  until nIndex = 0;

  // - - - Open the principal branch
  Sleep(25);
  SendMessageA(HWND_TreeView, WM_KEYDOWN, VK_RIGHT, 0);
  Sleep(25);
  nIndex := 1;
  nCount := Length(Key);

  // - - - Moves to the Key\SubKey (sent as parameter)
  repeat
    if Key[nIndex] = '\' then begin
      SendMessageA(HWND_TreeView, WM_KEYDOWN, VK_RIGHT, 0);
      Sleep(25);
    end else begin
      SendMessageA(HWND_TreeView, WM_CHAR, integer(Key[nIndex]), 0);
    end;
    nIndex := nIndex + 1;
  until nIndex = nCount;


  // * Set the focus on the [ListView] ( Values )
  HWND_ListView := FindWindowEx(HWND_REGEDIT, 0, PChar('SysListView32'), nil);
  SetForegroundWindow(HWND_ListView);

  if Length(Value) <> 0 then begin
    SendMessage(HWND_ListView, WM_KEYDOWN, VK_DOWN, 0);
    Sleep(500);

    // - - - Moves to the Value (sent as parameter)
    nIndex := 1;
    nCount := Length(Value);
    repeat
      SendMessage(HWND_ListView, WM_CHAR, integer(Value[nIndex]), 0);
      nIndex := nIndex + 1;
    until nIndex = nCount;
  end;

end;

end.


How to use? Adds the UnitRegEditJump to the uses clause in your form. See the sample code...

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  UnitRegEditJump,
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls;       


{ more code here }


procedure TForm1.Button3Click(Sender: TObject);
begin
{ jump to a Registry Value }
  TRegEditJump.JumpToKey('HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\SharedAccess\Parameters\FirewallPolicy\FirewallRules', 'WPDMTP-UPnP-Out-TCP');

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
{ jump to a Registry Key }
  TRegEditJump.JumpToKey('HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\SharedAccess\Parameters');

end;     

Foul implement the code to pass the key short name (eg. HKLM\bla bla\bla), but that's easier.

Regards!

 

TinyPortal © 2005-2018