Solved

get value type in a .reg file

Posted on 2011-02-20
7
641 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:Marco Gasi
  • 3
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:Thommy
Comment Utility
Why do you want to make a difference between DateTime and binary Data???

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:


var Reg: TRegistry;
    Buffer: Array[0..7] of Byte; 
begin
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_LOCAL_MACHINE; //Nur ein Beispiel... weiß ja nicht wo der Key sich befindet.
 Reg.OpenKey('YourRegKy',false);

 FillChar(Buffer,SizeOf(Buffer),0);
 Buffer[0] := $63;
 Buffer[1] := $7A;
 Buffer[2] := $C2;
 Buffer[3] := $BA;
 Buffer[4] := $53;
 Buffer[5] := $D2;
 Buffer[6] := $E3;
 Buffer[7] := $40;

 Reg.WriteBinaryData('DateTime',Buffer,SizeOf(Buffer));
 Reg.CloseKey; Reg.Free;
end;

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
Comment Utility
If you always use WriteBinaryData/ReadBinaryData to write and retrieve the values to and from the registry, then there is no need to distinguish between datetime and binary data...
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
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:
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;

Open in new window

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22422529.html
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Author Comment

by:Marco Gasi
Comment Utility
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
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
procedure TForm1.Button2Click(Sender: TObject);
begin
  try
    DateTimeToStr(HexToDateTime('637AC2BA53D2E340')); // 2/20/2011 2:47:49 PM
    //WriteDateTime();
  except
    //WriteBinaryData();
  end;
end;

Open in new window

0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
Comment Utility
You can try to convert the Hex to DateTime, in Try-Except block, if it succeeds then its a valid date and time otherwise it's a binary:
procedure TForm1.Button2Click(Sender: TObject);
begin
  try
    HexToDateTime('637AC2BA53D2E340');
    //WriteDateTime();
  except
    //WriteBinaryData();
  end;
end;

Open in new window

0
 
LVL 30

Author Closing Comment

by:Marco Gasi
Comment Utility
Thank a lot.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now