Marco Gasi
asked on
get value type in a .reg file
Hi, experts.
I have a reg file produced by my Delphi application, usin unit RegExpo (http://www.swissdelphicenter.ch/torry/showcode.php?id=1330)
The only one difernce between this file and a normal .reg file exported from within Registry Editor is this: after the = sign there is the type of value. So you can have something like
"DateTime"=hex:63,7A,C2,BA ,53,D2,E3, 40
"Binary"=hex:30,30,00,00
"MyString"="something stupid"
"MyInteger"=dword:00000019
Well. I wish to write data holded by this file in the Registry without using ShellExecute, but writing code on my own. My problem is to know when hex is referred to a binary value and I have to use WriteBinaryData and when hex is referred to a DateTime and I have to use WriteDateTime.
Any idea?
Thanks in advance.
I have a reg file produced by my Delphi application, usin unit RegExpo (http://www.swissdelphicenter.ch/torry/showcode.php?id=1330)
The only one difernce between this file and a normal .reg file exported from within Registry Editor is this: after the = sign there is the type of value. So you can have something like
"DateTime"=hex:63,7A,C2,BA
"Binary"=hex:30,30,00,00
"MyString"="something stupid"
"MyInteger"=dword:00000019
Well. I wish to write data holded by this file in the Registry without using ShellExecute, but writing code on my own. My problem is to know when hex is referred to a binary value and I have to use WriteBinaryData and when hex is referred to a DateTime and I have to use WriteDateTime.
Any idea?
Thanks in advance.
If you always use WriteBinaryData/ReadBinary Data to write and retrieve the values to and from the registry, then there is no need to distinguish between datetime and binary data...
Well you can check the length of the hex value, the dateTime is in eight bytes.
Or, you can use functions to convert the Hex to DateTime and if it has succeeded (valid datetime) then use WriteDateTime else WriteBinaryData:
Or, you can use functions to convert the Hex to DateTime and if it has succeeded (valid datetime) then use WriteDateTime else WriteBinaryData:
function HexToByte(s : String) : Byte;
const
cs = '0123456789ABCDEF';
begin
result := 0;
if (length(s) = 2) and
(s[1] in ['0'..'9','A'..'F']) and
(s[2] in ['0'..'9','A'..'F']) then
result := ((pos(s[1],cs)-1) *16) + (pos(s[2],cs)-1)
else raise EConvertError.CreateFmt('%s is not a Hexformatstring',[s]);
end;
function HexToFloat(s : String) : double; //HexToFloat
var i : Integer;
B : Array [0..7] of Byte;
begin
result := 0;
for i := 0 to 7 do B[i] := 0;
for i := 0 to SizeOf(result) - 1 do
B[I] := HexToByte(copy(s,(i*2)+1,2));
System.Move(B[0],result,SizeOf(result));
end;
function HexToDateTime(s : string) : TDateTime;
begin
result := HexToFloat(s);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
showmessage(DateTimeToStr(HexToDateTime('637AC2BA53D2E340'))); // 2/20/2011 2:47:49 PM
end;
https://www.experts-exchange.com/questions/22422529/HEX-TO-TDATETIME.html
ASKER
First of all, Thank you very mutch for your answers.
@Thommy, I don't agree: if I take a binary value and I write it with WriteBinaryData function but that valua was a originally a TDateTime, the value that I write in the registry will be not equal to the original one, so it's necessary to distinguish carefully if you don't want put in the registry wrong values.
@jimyX, a little question: what happens if I pass to HexToDateTime function a "wrong" value, that is a non TDateTime value? How can I check programatically if I have to use WriteDateTime or WriteBinaryData?
Cheers
@Thommy, I don't agree: if I take a binary value and I write it with WriteBinaryData function but that valua was a originally a TDateTime, the value that I write in the registry will be not equal to the original one, so it's necessary to distinguish carefully if you don't want put in the registry wrong values.
@jimyX, a little question: what happens if I pass to HexToDateTime function a "wrong" value, that is a non TDateTime value? How can I check programatically if I have to use WriteDateTime or WriteBinaryData?
Cheers
procedure TForm1.Button2Click(Sender: TObject);
begin
try
DateTimeToStr(HexToDateTime('637AC2BA53D2E340')); // 2/20/2011 2:47:49 PM
//WriteDateTime();
except
//WriteBinaryData();
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank a lot.
Hex is hex, so don't care about and always write the hex data with WriteBinaryData to registry!!!
You can write binary data to registry as follows:
Open in new window