?
Solved

Read/Write Int64 from/to the registry

Posted on 2006-07-19
8
Medium Priority
?
1,240 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 200 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 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