Jump to content

[Solved] Registry read out signed integer


Recommended Posts

Hi,

I wondered why negative integers I wrote into registry (e.g. negative x-coordinates of a gui if using two monitors and the right one is the main one) wouldn't return right when reading. Now I know: it is saved as an unsigned integer (without algebraic sign). So here is a snippet that is changing unsigned to signed integer:

Global Const $g_sRegKey = "HKEY_CURRENT_USER\Software\" & @ScriptName ; path to registry

RegWrite($g_sRegKey, "Value", "REG_DWORD", -2147483647) ; write some negative integer into registry; -2147483647 highest possible negative integer , 2147483648 highest possible positive integer if talking of 32bit

Local $sValue = RegRead($g_sRegKey, "Value") ; read out registry
ConsoleWrite("Value: " & $sValue & @CRLF) ; show real value in console

Local $sResult = _SignedInteger($sValue) ; change to signed value
ConsoleWrite("Result: " & $sResult & @CRLF) ; and show it in console


Func _SignedInteger($iUnsignedInteger)
    Local $iSignedInteger
    If $iUnsignedInteger > (2^31) Then ; then it means a negative integer
        $iSignedInteger = $iUnsignedInteger - (2^32)
    Else
        $iSignedInteger = $iUnsignedInteger
    EndIf
    Return $iSignedInteger
EndFunc

It took me some time to find out the problem and so I hope I can help somebody with this.

Regards, Conrad

Edited by Simpel
[Solved]
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

13 hours ago, Simpel said:

-2147483647 highest possible negative integer , 2147483648 highest possible positive integer if talking of 32bit

Wrong values: it's [-2147483648, 2147483647] for an Int32.

Besides, under x64, it indeed seems that while the registry correctly records the binary value of the DWORD, retrieving it yields an Int64 under AutoIt.

Things behave correctly if the value is a REG_QWORD (Int64).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd I have to disagree. When I try my example and change the value I write to registry to -2147483648 it will return +2147483648. Writing 2147483648 returns +2147483648.

Thanks for the other info. So next time I try REG_QWORD.

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

I was just noting that the range of a 2's complement 32-bit integer is [-2147483648, 2147483647].

Since 2147483648 exceeds the signed 32-bit range, it's quite natural that it stores and retrieves a 64-bit value identical to the input. That's an indication that the registry datatype (e.g. DWORD vs QWORD) isn't as strict as one could imagine.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...