Solved

Read/Write Int64 from/to the registry

Posted on 2006-07-19
8
1,220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i search if string exist in array ? 3 77
Delphi and Access based Enumeration 9 88
firemonkey Android Listview Sort items 7 102
Firemonkey BASS_Init into a thread 17 80
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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