views:

699

answers:

2

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

+1  A: 

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));
skamradt
Thanks for that - I still need to pad the string out with #00 to get the format the receiving application requires, but this is bang on!
Dan Kelly
+4  A: 

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.

Rob Kennedy
Unfortunately not the case. Changing my code to use WriteString changes the key in the registry to a REG_SZ key, which the receiving application cannot write. RegEdit displays the REG_SZ key as plain text not the hex value seen when looking at the detail entered by the other program.StrToInt on a true text value blows up of course
Dan Kelly