2003. december 28., vasárnap

How to read binary values from the registry


Problem/Question/Abstract:

I want to read out the binary value "problems" of the path HKEY_DYN_DATA\Config Manager\Enum\[add the key of a hardware component] to detect if a hardware component is troubled and not working right. But I cannot handle the ReadBinaryData-Method of TRegistry correct. Everytime I use it, it always returns "4" as content of the buffer. How do I detect if the content of the binary key "problems" is not "00 00 00 00" but something else like "16 00 00 00" or such?

Answer:

Here's an example of ReadBinaryData:


procedure TFrmReadBinary.Button1Click(Sender: TObject);
const
  CKeyName: string = 'System\Setup';
  CValName: string = 'NetcardDlls';
var
  keyGood: boolean;
  p: integer;
  regKey: TRegistry;
  tmpStr: string;
  vSize: integer;
begin
  regKey := TRegistry.Create;
  try
    regKey.RootKey := HKEY_LOCAL_MACHINE;
    keyGood := regKey.OpenKey(CKeyName, false);
    if (keyGood) then
    begin
      vSize := regKey.GetDataSize(CValName);
      if (vSize > 0) then
      begin
        SetLength(tmpStr, vSize);
        regKey.ReadBinaryData(CValName, tmpstr[1], vSize);
        repeat
          p := Pos(#0, tmpStr);
          if p <> 0 then
          begin
            Delete(tmpStr, p, 1);
            Insert(#13#10, tmpStr, p);
          end;
        until
          p = 0;
        {StringReplace(tmpStr, #0, #13#10, [rfReplaceAll]);}
        ListBox1.Items.Text := tmpStr;
      end;
    end;
  finally
    regKey.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése