Delphi - 将字符串写入Reg_Binary注册表项

Dan*_*lly 2 delphi registry

我需要转换一个字符串,将其写入registry.reg_binary键.

我有写入密钥的基本代码如下:

try
   rootkey := HKEY_CURRENT_USER;
   if OpenKey(Key, False) then
   begin
      reg.WriteBinaryData('SomeKey', SomeValue, Length(SomeVale));
      CloseKey;
   end;
finally
   reg.Free;
end;
Run Code Online (Sandbox Code Playgroud)

在上面,SomeValue需要是TEdit文本字段的十六进制值;

我目前的方法是使用IntToHex在每个字符的Ord值上转换TEdit.text.这给了我一个看起来像我想写的字符串......

此时我很难过......

Rob*_*edy 6

如果你想写一个字符串,那么你应该打电话WriteString.

reg.WriteString('SomeKey', SomeValue);
Run Code Online (Sandbox Code Playgroud)

如果你有一个整数,那么调用WriteInteger.

IntValue := StrToInt(SomeValue);
reg.WriteInteger('SomeKey', IntValue);
Run Code Online (Sandbox Code Playgroud)

如果你有真正的二进制数据,那么它应该是什么样子 - 十六进制或其他什么.打电话WriteBinaryData来完成它.数据的实际外观并不重要,因为您不必以该格式读取数据.稍后您将阅读它ReadBinaryData,它将以您编写它们时所具有的格式填充缓冲区中的字节.

IntValue := StrToInt(SomeValue);
reg.WriteBinaryValue('SomeKey', IntValue, SizeOf(IntValue));
Run Code Online (Sandbox Code Playgroud)

这会将整数的所有四个字节写入注册表中作为二进制值.

当Windows注册表编辑器向您显示键的值时,它可能会以十六进制显示每个字节,但这只是一种显示格式.这并不意味着您必须在将数据添加到注册表之前将其格式化.