使用delphi中的Tregistry Class将reg_binary读为字符串

时间:2016-02-23 13:04:47

标签: delphi registry

我试图从注册表项中将reg_binary作为字符串。

这是我的功能

(function () {

  angular.module('module')

    .controller('controller', [
      '$scope', '$ionicModal', '$timeout',
      function ($scope, $ionicModal, $timeout) {

    ...

    }]);
})();

这是我的注册表项导出:

function ReadBinString(key: string; AttrName: string): string;
var
 ReadStr: TRegistry;

begin
// Result := '';
ReadStr := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY);
ReadStr.RootKey := HKEY_LOCAL_MACHINE;

   if ReadStr.OpenKey(key, true) then
begin

  Result := ReadStr.GetDataAsString(AttrName);
end;

ReadStr.CloseKey;
ReadStr.Free;
end;
问题是,该函数返回空字符串

我甚至尝试以管理员身份运行,以确保它不是权限。

任何帮助?

2 个答案:

答案 0 :(得分:4)

扩展我对该问题的评论,我会使用这样的代码:

function ReadBinString(RootKey: HKEY; Access: LongWord; const KeyName,
  ValueName: string; Encoding: TEncoding): string;
var
  Registry: TRegistry;
  Bytes: TBytes;
begin
  Registry := TRegistry.Create(Access);
  try
    Registry.RootKey := RootKey;
    if Registry.OpenKeyReadOnly(KeyName) then begin
      SetLength(Bytes, Registry.GetDataSize(ValueName));
      Registry.ReadBinaryData(ValueName, Pointer(Bytes)^, Length(Bytes));
      Result := Encoding.GetString(Bytes);
    end else begin
      Result := '';
    end;
  finally
    Registry.Free;
  end;
end;

对于您的数据,您可以这样称呼它:

Value := ReadBinString(HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY, 'Software\ZES\ACINFO', 
  'ArrayOrder', TEncoding.ANSI);

注意:

  • 我避免了对根密钥进行硬编码。
  • 我使用TEncoding将字节数组解码为文本。这比GetDataAsString更有效。
  • 我已允许调用者指定要使用的编码。
  • 我已允许调用者指定访问标记。
  • 我使用了OpenKeyReadOnly因为我们不需要写访问权。

答案 1 :(得分:0)

感谢David Heffernan我带来了这个解决方案:

function ReadBinString(key: string; AttrName: string): string;
var
ReadStr: TRegistry;
hexStr : string;
I : Integer;
begin
// Result := '';
 ReadStr := TRegistry.Create(KEY_READ OR KEY_WOW64_64KEY);
 ReadStr.RootKey := HKEY_LOCAL_MACHINE;

 if ReadStr.OpenKey(key, true) then
 begin

hexStr := ReadStr.GetDataAsString(AttrName);

hexStr := hexStr.Replace(',','');
for I := 1 to length (hexStr) div 2 do
Result:= Result+Char(StrToInt('$'+Copy(hexStr,(I-1)*2+1,2)));

end;

ReadStr.CloseKey;
ReadStr.Free;
end;

再次感谢David Heffernan ......这对我有用:

    function ReadBinString(key: string; AttrName: string): string;
    var
      ReadStr: TRegistry;
      hexStr: string;
      I: Integer;
      Bytes: TBytes;
      Encoding: TEncoding;
    begin

      Encoding :=  TEncoding.ANSI;

      Result := '';
      ReadStr := TRegistry.Create(KEY_READ OR KEY_WOW64_64KEY);
      ReadStr.RootKey := HKEY_LOCAL_MACHINE;
      try

        if ReadStr.OpenKeyReadOnly(key ) then
        begin

          SetLength(Bytes, ReadStr.GetDataSize(AttrName));
          ReadStr.ReadBinaryData(AttrName, Pointer(Bytes)^, Length(Bytes));
          Result := Encoding.GetString(Bytes);

          // hexStr := ReadStr.GetDataAsString(AttrName);
          //
          // hexStr := hexStr.Replace(',','');
          // for I := 1 to length (hexStr) div 2 do
          // Result:= Result+Char(StrToInt('$'+Copy(hexStr,(I-1)*2+1,2)));

        end;

      except

      end;
      ReadStr.CloseKey;
      ReadStr.Free;
    end;