Search code examples
delphidelphi-2009delphi-2010

How can I remotely read binary registry data using Delphi 2010?


I am trying to remotely read a binary (REG_BINARY) registry value, but I get nothing but junk back. Any ideas what is wrong with this code? I'm using Delphi 2010:

function GetBinaryRegistryData(ARootKey: HKEY; AKey, AValue, sMachine: string; var sResult: string): boolean;
    var
      MyReg: TRegistry;
      RegDataType: TRegDataType;
      DataSize, Len: integer;
      sBinData: string;
      bResult: Boolean;
    begin
      bResult := False;
      MyReg := TRegistry.Create(KEY_QUERY_VALUE);

      try
        MyReg.RootKey := ARootKey;
        if MyReg.RegistryConnect('\\' + sMachine) then
        begin
          if MyReg.KeyExists(AKey) then
          begin
            if MyReg.OpenKeyReadOnly(AKey) then
            begin
              try
                RegDataType := MyReg.GetDataType(AValue);
                if RegDataType = rdBinary then
                begin
                  DataSize := MyReg.GetDataSize(AValue);
                  if DataSize > 0 then
                  begin
                    SetLength(sBinData, DataSize);
                    Len := MyReg.ReadBinaryData(AValue, PChar(sBinData)^, DataSize);
                    if Len <> DataSize then
                      raise Exception.Create(SysErrorMessage(ERROR_CANTREAD))
                    else
                    begin
                      sResult := sBinData;
                      bResult := True;
                    end;
                  end;
                end;
              except
                MyReg.CloseKey;
              end;
              MyReg.CloseKey;
            end;
          end;
        end;
      finally
        MyReg.Free;
      end;

      Result := bResult;
    end;

And I call it like this:

GetBinaryRegistryData(
   HKEY_LOCAL_MACHINE, 
   '\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 
   'DigitalProductId', '192.168.100.105', 
   sProductId
);

WriteLn(sProductId);

The result I receive from the WriteLn on the console is:

ñ ♥ ???????????6Z ????1   ???????☺  ???♦            ??3   ? ??? ?
??

Solution

  • Assuming that you are already connected remotely, try using the GetDataAsString function to read binary data from the registry.

    sResult := MyReg.GetDataAsString(AValue);