Link to home
Start Free TrialLog in
Avatar of cebasso
cebassoFlag for United States of America

asked on

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

Avatar of 8080_Diver
8080_Diver
Flag of United States of America image

Why do you need to set the datatime as binary?  Wouldn't it make more sense to set it as a string?
Avatar of cebasso

ASKER

@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...
Avatar of cebasso

ASKER

another thing... my idea is block the user to change this date...
setting as String, he can easyly change...
Avatar of Geert G
just use the function DateTimeToFileDate(DateTime: TDateTime): integer;
and store this integer in the registry with WriteInteger
off course, the opposite works too
function FileDateToDateTime(FileDate: Integer): TDateTime;
No rocket science ...
Avatar of cebasso

ASKER

@Geert_Gruwez
humm nice idea, but this mini reg doesn't have a Integer option, just
String,
ExpandString,
MultiString,
Dword,
Binary
:(
ASKER CERTIFIED SOLUTION
Avatar of JosephGlosz
JosephGlosz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.



Avatar of cebasso

ASKER

Wow Man, perfectly!!!
:DDD

Thank you very much!!

Best Regards
Carlos
:D
Avatar of cebasso

ASKER

@JosephGlosz
Perfect!!
Wow thank you very much! :D
Best Regards,
Carlos
@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
for the dummies:
a DateTimeToFileDate also contains a time portion and is maintained in the integer !!!
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.


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


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
Avatar of cebasso

ASKER

@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

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