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
Solved

Read/Write Int64 from/to the registry

Posted on 2006-07-19
8
1,200 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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
Delphi...Split view - idea? 1 88
Delphi and Access based Enumeration 9 70
enhance the following code 3 37
Firemonkey allowing RTL on android 6 46
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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