unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WinSvc;

type
  TfMain = class(TForm)
    lb: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

procedure TfMain.FormCreate(Sender: TObject);
var RK, RSK : HKey;
    Ix, DW, S, N, T, NameSize : DWORD;
    Name, Descr : PWideChar;
    fr : longint;
const NSize = 500;
begin
  RegOpenKeyEx(HKEY_CURRENT_USER, 'Software\IC\RegDemo', 0, KEY_READ, RK);
  S := SizeOf(DWORD);
  if RegQueryValueEx(RK, 'Left', nil, nil, @DW, @S) = ERROR_SUCCESS then
    Left := DW;
  if RegQueryValueEx(RK, 'Top', nil, nil, @DW, @S) = ERROR_SUCCESS then
    Top := DW;
  if RegQueryValueEx(RK, 'Width', nil, nil, @DW, @S) = ERROR_SUCCESS then
    Width := DW;
  if RegQueryValueEx(RK, 'Height', nil, nil, @DW, @S) = ERROR_SUCCESS then
    Height := DW;
  RegCloseKey(RK);

  RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services', 0,
    KEY_ENUMERATE_SUB_KEYS, RK);
  Ix := 0;
  GetMem(Name, NSize);
  GetMem(Descr, NSize);
  NameSize := NSize;
  while RegEnumKeyEx(RK, Ix, Name, NameSize, nil, nil, nil, nil) = ERROR_SUCCESS  do begin
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, PWideChar('SYSTEM\CurrentControlSet\Services\' + string(Name)), 0,
      KEY_ALL_ACCESS, RSK);
    N := NSize;
    fr := RegQueryValueEx(RSK, 'DisplayName', nil, nil, PByte(Descr), @N);
    if (fr = ERROR_SUCCESS) and (N > 0) and (Descr <> '') then begin
      N := SizeOf(T);
      fr := RegQueryValueEx(RSK, 'Type', nil, nil, @T, @N);
      if (fr = ERROR_SUCCESS) and (N > 0) and (T = SERVICE_WIN32_OWN_PROCESS) then
        lb.Items.Add(string(Descr));
    end;
    RegCloseKey(RSK);
    NameSize := NSize;
    Inc(Ix);
  end;
  FreeMem(Name);
  FreeMem(Descr);
  RegCloseKey(RK);
end;

procedure TfMain.FormDestroy(Sender: TObject);
var RK : HKey;
    L, T, W, H  : DWORD;
begin
  L := Left;      T := Top;
  W := Width;     H := Height;
  RegCreateKeyEx(HKEY_CURRENT_USER, 'Software\IC\RegDemo', 0, nil, 0, KEY_ALL_ACCESS, nil, RK, nil);
  RegSetValueEx(RK, 'Left', 0, REG_DWORD, @L, SizeOf(DWORD));
  RegSetValueEx(RK, 'Top', 0, REG_DWORD, @T, SizeOf(DWORD));
  RegSetValueEx(RK, 'Width', 0, REG_DWORD, @W, SizeOf(DWORD));
  RegSetValueEx(RK, 'Height', 0, REG_DWORD, @H, SizeOf(DWORD));
  RegCloseKey(RK);
end;

end.
