Delphi - Write string to Reg_Binary registry key
I need to convert a string to write it into a registry.reg_binary key.
I have the basic code for writing into the key as follows:
try
rootkey := HKEY_CURRENT_USER;
if OpenKey(Key, False) then
begin
reg.WriteBinaryData('SomeKey', SomeValue, Length(SomeVale));
CloseKey;
end;
finally
reg.Free;
end;
In the 开发者_如何转开发above, SomeValue needs to be the hex value of a TEdit text field;
My current tack is convert the TEdit.text using IntToHex on the Ord value of each character. This gives me a string that looks like what I want to write...
At this point I'm stumped...
If you want to write a string, then you should call WriteString
.
reg.WriteString('SomeKey', SomeValue);
If you have an integer, then call WriteInteger
.
IntValue := StrToInt(SomeValue);
reg.WriteInteger('SomeKey', IntValue);
If you have true binary data, then it shouldn't matter what it looks like — hexadecimal or whatever. Call WriteBinaryData
and be done with it. The actual appearance of the data is immaterial because you don't have to read it in that format. You'll read it later with ReadBinaryData
and it will fill your buffer with the bytes in whatever format they had when you wrote them.
IntValue := StrToInt(SomeValue);
reg.WriteBinaryValue('SomeKey', IntValue, SizeOf(IntValue));
That will write all four bytes of your integer into the registry as a binary value.
When the Windows Registry Editor displays the key's value to you, it will probably display each byte in hexadecimal, but that's just a display format. It doesn't mean you have to format your data like that before you add it to the registry.
Granted this assumes that your string only contains ansi data, but if your trying to write a string to a registry value as a binary value then the following changes to your logic would work:
var
EdStr : AnsiString;
:
EdStr := AnsiString(Edit1.Text); // de-unicode
reg.WriteBinaryData('SomeKey', EdStr[1], Length(EdStr));
精彩评论