2006. május 2., kedd

Read a registry entry of type REG_MULTI_SZ


Problem/Question/Abstract:

How to read a registry entry of type REG_MULTI_SZ

Answer:

REG_MULTI_SZ is a registry type holding multiple strings. This function reads the value into a TStrings object and returns true if successful.

function ReadREG_MULTI_SZ(const HK: HKey; const k, v: string; ts: TStrings): boolean;
var
  vType: DWORD;
  vLen: DWORD;
  p, buffer: PChar;
  key: HKEY;
begin
  result := false;
  ts.Clear;
  if (RegOpenKeyEx(HK, PChar(k), 0, KEY_READ, key) = ERROR_SUCCESS) then
  begin
    SetLastError(RegQueryValueEx(key, PChar(v), nil, @vType, nil, @vLen));
    if (GetLastError = ERROR_SUCCESS) and (vType = REG_MULTI_SZ) then
    begin
      GetMem(buffer, vLen);
      try
        RegQueryValueEx(key, PChar(v), nil, nil, PBYTE(buffer), @vLen);
        p := buffer;
        while (p^ <> #0) do
        begin
          ts.Add(p);
          Inc(p, lstrlen(p) + 1)
        end;
      finally
        FreeMem(buffer)
      end;
      result := true;
    end;
  end;
end;

Example to read the 'Pagingfiles' value into a TMemo:

procedure TForm1.Button1Click(Sender: TObject);
var
  k, val: string;
begin
  k := 'SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management';
  val := 'Pagingfiles';
  ReadREG_MULTI_SZ(HKEY_LOCAL_MACHINE, k, val, Memo1.Lines);
end;

Nincsenek megjegyzések:

Megjegyzés küldése