Solved

Read/Write Int64 from/to the registry

Posted on 2006-07-19
8
1,163 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
Comment Utility
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
Comment Utility

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
Comment Utility
Thanks Rick.

LongWord(MyRegInt) did the trick. Int64(MyRegInt) was still retuning the negative value btw.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
Comment Utility
opps...yeah because int64 is signed...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
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
Comment Utility
My bad, should have been one of:

l: LongWord;

  l:=LongWord(readInteger(name))

or

 Integer(l):=readInteger(name)

Russell
0
 

Author Comment

by:smartins
Comment Utility
Thanks Russell for the clarifications.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now