lightweight replacement for TRegistry - How to set binary?

Hello,

i got a Unit where it is a TRegistry replacement, to make more clean and lightweight access to the Windows Registry...

But, i was trying to set a TDateTime value as Binary and no success

You can get this Unit at
http://www.swissdelphicenter.ch/torry/showcode.php?id=2008

Someone has any ideia how to do that?

example below

Best Regards,
Carlos
I want to set a TDateTime value as binary using this Mini reg
 
var
  dt: TDateTime;
begin
  dt := Now;
  //now using that mini reg, set this TDateTime to registry
  RegSetBinary(HKEY_CURRENT_USER, 'Software\Test\dt', ?!?!
 
  //and read too...
  RegGetBinary(?!?!
  //the output value at this function is String... how to read?!

Open in new window

LVL 4
cebassoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

8080_DiverCommented:
Why do you need to set the datatime as binary?  Wouldn't it make more sense to set it as a string?
0
cebassoAuthor Commented:
@8080_Diver
Humm, i think about... but my idea to set as binary comes from
http://delphi.about.com/cs/adptips2002/a/bltip1002_4.htm
But it use TRegistry, and i'd like to use this lightweight Mini Reg...
0
cebassoAuthor Commented:
another thing... my idea is block the user to change this date...
setting as String, he can easyly change...
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Geert GOracle dbaCommented:
just use the function DateTimeToFileDate(DateTime: TDateTime): integer;
and store this integer in the registry with WriteInteger
0
Geert GOracle dbaCommented:
off course, the opposite works too
function FileDateToDateTime(FileDate: Integer): TDateTime;
No rocket science ...
0
cebassoAuthor Commented:
@Geert_Gruwez
humm nice idea, but this mini reg doesn't have a Integer option, just
String,
ExpandString,
MultiString,
Dword,
Binary
:(
0
JosephGloszCommented:
TDateTime is a double, which is 8 bytes in size. This'll work great if you simply overlay the TDateTime type with a byte array type as follows.

var
  dt,dt2: TDateTime;
  dtBytes: array[1..8] of byte absolute dt;
  dtBytes2: array[1..8] of byte absolute dt2;
  s: string;
  i: integer;
begin
  dt := Now;  // automatically sets the dtBytes variable since it occupies same location
  RegSetBinary(HKEY_CURRENT_USER, 'Software\Test\MyDateTime',dtBytes);

  // the 'get' is trickier since it apparently requires a string.
 
  RegGetBinary(HKEY_CURRENT_USER, 'Software\Test\MyDateTime', s);
  if length(s) = 8 then
    begin
    for i := 1 to 8 do dtBytes2[i] := ord(S[i]);
    // now dt2 has the datetime in it already....
    // do with it as you will...
    end
  else
    // show some kind of error


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JosephGloszCommented:
@Geert:  The request was to store a TDateTime, not just a date.  Even if your integer storage worked, how were you planning to store the time part?  Remember, in a TDateTime, the date is the integer part of the double, and the time is the fractional part.

So if you already had the datetime, which the author already has (he said nothing about using a filedate) you sure don't even need your DateTimeToFileDate function.  If you wanted the integer, just use the Trunc function to get the integer part of the TDateTime after some appropriate casting.



0
cebassoAuthor Commented:
Wow Man, perfectly!!!
:DDD

Thank you very much!!

Best Regards
Carlos
:D
0
cebassoAuthor Commented:
@JosephGlosz
Perfect!!
Wow thank you very much! :D
Best Regards,
Carlos
0
Geert GOracle dbaCommented:
@JosephGlosz
I don't like to be told i'm wrong by somebody who obviously has no clue as what they are talking about.
difference between you and a real expert i guess ...

You are saying the sample below will produce the same results ?
try it and be stunned ...
procedure TForm1.Button1Click(Sender: TObject);
var X: Integer;
begin
  X := Trunc(Now - 5);
  ShowMessage(DateTimeToStr(X));
  X := DateTimeToFileDate(Now - 5);
  ShowMessage(DateTimeToStr(FileDateToDateTime(X)));
end;

@Carlos
procedure TForm1.Button1Click(Sender: TObject);
var X: Integer;
begin
  X := DateTimeToFileDate(Now - 5);
  //ShowMessage(DateTimeToStr(FileDateToDateTime(X)));
  //Just write the X as a string to the registry
  RegSetString(HKEY_CURRENT_USER, 'Software/ForJGlos', IntToStr(X));
end;

no fancy code needed :p
0
Geert GOracle dbaCommented:
for the dummies:
a DateTimeToFileDate also contains a time portion and is maintained in the integer !!!
0
JosephGloszCommented:
for other dummies:
care to show how the DateTimeToFileDate will encode, say, the milliseconds portion of a TDateTime?

show me that, and you too can be a "real" REAL expert!

but yes, the FileDate will store down time down to the nearest two seconds or so? Is that right? I *did* forget that it does store that much of the time.   So the next time you want to answer a different question than what's being asked, I guess you're all set.


0
JosephGloszCommented:
since we are posted nice and neat example code, here's one for all those who like their TDateTime's rounded to the nearest two seconds:

put a Tmemo on a form, and a button, then put the following in the button:

procedure TForm1.Button1Click(Sender: TObject);
var
  X: Integer;
begin
  X := DateTimeToFileDate(Now);
  Memo1.Lines.Add(FormatDateTime('mm/dd/yyyy hh:nn:ss:zzz',FileDateToDateTime(X)));

  inc(X);
  Memo1.Lines.Add(FormatDateTime('mm/dd/yyyy hh:nn:ss:zzz',FileDateToDateTime(X)));

  inc(X);
  Memo1.Lines.Add(FormatDateTime('mm/dd/yyyy hh:nn:ss:zzz',FileDateToDateTime(X)));

  inc(X);
  Memo1.Lines.Add(FormatDateTime('mm/dd/yyyy hh:nn:ss:zzz',FileDateToDateTime(X)));
end;

What this is doing is incrementing the integer by ONE then displays the equivalent TDateTime in the memo box.

You'll get these results:

05/27/2009 14:04:44:000
05/27/2009 14:04:46:000
05/27/2009 14:04:48:000
05/27/2009 14:04:50:000

So, if anyone truly wants to record a very low resolution version of a TDateTime, only managing a 2-second window, then by all means, go with the REAL EXPERT Geert, the only one with a real clue as to the best way to do something. Especially when "no fancy code" is needed.   :p


0
Geert GOracle dbaCommented:
this lightweight actually uses just the same functions as the borland TRegistry class does
Borland did it in a nice OOP way, this lightweight is just a bunch of procedures

i would stick to the OOP way

next to that, there is no 64-bit support in this lightweight
0
cebassoAuthor Commented:
@Geert_Gruwez
64 bits mean you about WoW6432Node?
if Yes i did my own support like the code below...
isWow64 check if the system is running "Windows on Windows"

My Windows version is Vista 64 Bits (AMD64Bits wow64), i did a lot of tests and no problems...
I have tested on another Machine with the same Windows but Intel and no problems too
I hope they dont appear!! hehe
:D
Thank you for reply!

function RegSetValue(RootKey: HKEY; Name: String; ValType: Cardinal; PVal: Pointer; ValSize: Cardinal; Wow64: Boolean): boolean;
var
  SubKey: String;
  n: integer;
  dispo: DWORD;
  hTemp: HKEY;
  bSuccess: Boolean;
  _hKey: Cardinal;
begin
  Result := False;
  
  n := LastPos('\', Name);
  if n > 0 then
  begin
    SubKey := Copy(Name, 1, n - 1);
 
    if Wow64 and IsWow64 then
    _hKey := KEY_WRITE or KEY_WOW64_64KEY else
    if IsWow64 and not Wow64 then
    _hKey := KEY_WRITE or KEY_WOW64_32KEY else
    _hKey := KEY_WRITE;
 
    bSuccess := RegCreateKeyEx(RootKey, PChar(SubKey), 0, nil, REG_OPTION_NON_VOLATILE, _hKey, nil, hTemp, @dispo) = ERROR_SUCCESS;
 
    if bSuccess then
    begin
      SubKey := Copy(Name, n + 1, Length(Name) - n);
      Result := (RegSetValueEx(hTemp, PChar(SubKey), 0, ValType, PVal, ValSize) = ERROR_SUCCESS);
      RegCloseKey(hTemp);
    end;
  end;
end;

Open in new window

0
Geert GOracle dbaCommented:
the problem i encountered was with a 32 bit application and driver on 64 bit terminal server
not a really big issue
the settings were being looked for in the wrong place in the registry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.