如何使用 Delphi 2010 远程读取二进制注册表数据?

发布于 2024-09-27 07:40:27 字数 1997 浏览 0 评论 0原文

我试图远程读取二进制 (REG_BINARY) 注册表值,但除了垃圾之外什么也没有得到。有什么想法这段代码有什么问题吗?我正在使用 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;

我这样称呼它:

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

WriteLn(sProductId);

我从控制台上的 WriteLn 收到的结果是:

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

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   ? ??? ?
??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-10-04 07:40:27

假设您已经远程连接,请尝试使用 GetDataAsString 函数
从注册表读取二进制数据。

sResult := MyReg.GetDataAsString(AValue);

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

sResult := MyReg.GetDataAsString(AValue);
快乐很简单 2024-10-04 07:40:27

您使用的是 Delphi 2010,因此所有字符都是两个字节宽。当您设置结果字符串的长度时,您将分配所需空间量的两倍。然后您调用ReadBinaryData,它会填充您的缓冲区的一半。每个字符有两个字节的数据。单独查看每个字节,您可能会发现您的数据看起来不像垃圾。

不要使用字符串来存储任意数据。使用字符串来存储文本。要存储任意 blob 数据,请使用 TBytes,它是一个字节数组。

You're using Delphi 2010, so all your characters are two bytes wide. When you set the length of your result string, you're allocating twice the amount of space you need. Then you call ReadBinaryData, and it fills half your buffer. There are two bytes of data in each character. Look at each byte separately, and you'll probably find that your data looks less garbage-like.

Don't use strings for storing arbitrary data. Use strings for storing text. To store arbitrary blobs of data, use TBytes, which is an array of bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文