Solved

Read/Write Int64 from/to the registry

Posted on 2006-07-19
8
1,212 Views
Last Modified: 2008-01-09
I can't seem to find any function that reads/writes Int64 numbers from/to the registry.

I currently have a number there that was saved using WriteInteger (2167501190) that reads as -2127466106. It's odd that a number greater that Integer was written using WriteInteger.

Anyway, is there any function that I can use to read/write Int64 numbers from/to the registry?
I could use the ReadFloat/WriteFloat but that means my value will change from integer to Binary and I was looking for a possible easier solution.

Thanks,
Steven
0
Comment
Question by:smartins
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 50 total points
ID: 17138932
Steven,
The following is an example of how the TRegistry class can be extended to read/write int64 values (REG_QWORD). I included the class extension in with the form code example, but obviously this can be split out if desired.

Regards,
Russell

---

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Registry, StdCtrls, Consts;

const
  REG_QWORD         =  11;

type
  TRegistryEx       =  class(TRegistry)
  public
     function       ReadInt64(const Name: String): Int64;
     procedure      WriteInt64(const Name: String; Value: Int64);
  end;


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function TRegistryEx.ReadInt64(const Name: String): Int64;
var  dwType:        DWORD;
     dwSize:        DWORD;
begin
  dwType:=REG_NONE;
  dwSize:=SizeOf(Int64);
  if (RegQueryValueEx(CurrentKey, PChar(Name), nil, @dwType, PByte(@result), @dwSize) <> ERROR_SUCCESS) then
     raise ERegistryException.CreateResFmt(@SRegGetDataFailed, [Name]);
  if (dwType <> REG_QWORD) then raise ERegistryException.CreateResFmt(@SInvalidRegType, [Name]);
end;

procedure TRegistryEx.WriteInt64(const Name: String; Value: Int64);
var  dwType:        DWORD;
     dwSize:        DWORD;
begin
  dwType:=REG_QWORD;
  dwSize:=SizeOf(Int64);
  if (RegSetValueEx(CurrentKey, PChar(Name), 0, dwType, @Value, dwSize) <> ERROR_SUCCESS) then
     raise ERegistryException.CreateResFmt(@SRegSetDataFailed, [Name]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var  regEx:         TRegistryEx;
     qwValue:       Int64;
begin

  regEx:=TRegistryEx.Create;
  try
     regEx.RootKey:=HKEY_CURRENT_USER;
     regEx.OpenKey('\Software\Int64Testing', True);
     qwValue:=10000;
     regEx.WriteInt64('Int64Value', qwValue);
     qwValue:=regEx.ReadInt64('Int64Value');
     if (qwValue <> 10000) then
        ShowMessage('Failed to read data back in correctly!');
  finally
     regEx.Free;
  end;

end;

end.
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 75 total points
ID: 17139386

The value you have is actually a 32bit value signed...you just need to cast it as a 32bit value unsigned...

Try this

var
  a_LongWord: LongWord;
  a_Int64: Int64;

  a_LongWord := LongWord(MyRegInt);
  a_Int64 := Int64(MyRegInt);

either of these will work...since the regristry value you have did not go over 32 bits...in Integer the most significant bit carries the sign...and thats why you have a negative value...

Rick Peterson
 

 
0
 

Author Comment

by:smartins
ID: 17139490
Thanks Rick.

LongWord(MyRegInt) did the trick. Int64(MyRegInt) was still retuning the negative value btw.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17140033
opps...yeah because int64 is signed...
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17140097
Yep, int64 is signed. And Steven, you need to ensure that if you are writing unsigned values that they are read back in to a variable that is the same type used to write it.

eg
 l: LongWord;
 j: Integer;

 writeInteger(name, l)
 j:=readInteger(name)

will result in incorrect results, as will

 writeinteger(name, j)
 l:=readInteger(name)

----

Russell


0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17140142
My bad, should have been one of:

l: LongWord;

  l:=LongWord(readInteger(name))

or

 Integer(l):=readInteger(name)

Russell
0
 

Author Comment

by:smartins
ID: 17140521
Thanks Russell for the clarifications.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17140998
if you want to shove the Integer into an Int64 do this...

  a_Int64 := Int64(a_Integer);
  a_Int64 := a_Int64  and $FFFFFFFF;

the int64 will now only hold the first 32bits...0 out the other 32bits...(which are holding the sign)

the interesting thing I found...

when you type anything with a significant bit flipped...the container(int64) flips all the rest of the bits on...
1111111111111111111111111111111110000001001100010111000110000110 - int64
                                                        10000001001100010111000110000110 - integer and longword

So we are going to AND the value against our mask
1111111111111111111111111111111110000001001100010111000110000110 - int64
0000000000000000000000000000000011111111111111111111111111111111 - $FFFFFFFF

0000000000000000000000000000000010000001001100010111000110000110 - int64 after AND

Hope this helps to clarify whats going on...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle global variables 4 79
Controlled Assessment GCSE - desperate help needed 4 110
Create a path if not exists 7 106
Delphi android app hide keyboard 3 39
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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