Jump to content

Bug with RegRead?


Recommended Posts

Is it just me, or is RegRead unable to read a value of 0 (zero)?

The following code writes a value ($DataIn) or type REG_DWORD and then reads it back again. It works for any value for $DataIn except 0, where it returns a "". I've tested in 3.2.4.9

Or is this normal behaviour?

const $cRegKey = "HKLM\Software\TestKey"
const $cRegVal = "TestValue"
const $cRegType = "REG_DWORD"

$DataIn = 0

if RegWrite($cRegKey, $cRegVal, $cRegType, $DataIn) <> 1 then MsgBox(0, "RegWrite", "Error " & @error)

$DataOut = RegRead($cRegKey, $cRegVal)
$ErrValue = @error

if $DataOut = "" then MsgBox(0, "RegRead", "Error " & $ErrValue)
Link to comment
Share on other sites

Why don't you replace

if $DataOut = "" then MsgBox(0, "RegRead", "Error " & $ErrValue)

with

MsgBox(0, "RegRead", $DataOut)

and see for yourself, instead of making incorrect assumptions?

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Is it just me, or is RegRead unable to read a value of 0 (zero)?

Seems to be dependant on the registry value type. e.g. REG_DWORD, REG_SZ...

; REG_BINARY returns a number
; REG_SZ returns a string
; REG_MULTI_SZ returns a string
; REG_EXPAND_SZ returns a string
; REG_DWORD returns a number

Const $cRegKey = "HKLM\Software\TestKey"
Const $cRegVal = "TestValue"
Const $RegType[5] = ['REG_BINARY', 'REG_SZ', 'REG_MULTI_SZ', 'REG_EXPAND_SZ', "REG_DWORD"]

$DataIn = 0

For $i = 0 To UBound($RegType) -1
    If Not RegWrite($cRegKey, $cRegVal, $RegType[$i], $DataIn) Then
        ConsoleWrite('test' & ($i + 1) & ' Error' & @CRLF)
    Else
        ConsoleWrite('test' & ($i + 1) & ' ' & IsString(RegRead($cRegKey, $cRegVal))& @CRLF)
    EndIf
Next

Or is this normal behaviour?

Hmmm, I was would have expected all strings returned like IniRead returns but seems different with using RegRead with the registry type being read.

So unsure to say a bug when the types that are not strings do not end the the "_SZ" type. "_SZ" refers as being a string registry type so perhaps the non string returns are correct.

:whistle:

Link to comment
Share on other sites

And if you want to compare a number or other non-string with a string "", better use == instead of =

That is not correct. The operator '==' in AutoIt is only useful for case-sensitive string compares. It does not modify the behavior of AutoIt's automatic conversion of string numbers when comparing to actual numbers:

$Test1 = "a" = "a"
ConsoleWrite('"a" = "a": ' & $Test1 & @LF)
$Test1 = "1" = "01"
ConsoleWrite('"1" = "01": ' & $Test1 & @LF)
$Test1 = 1 = "1"
ConsoleWrite('1 = "1": ' & $Test1 & @LF)
$Test1 = 1 == "1"
ConsoleWrite('1 == "1": ' & $Test1 & @LF)
$Test1 = 1 = "001"
ConsoleWrite('1 = "001": ' & $Test1 & @LF)

:whistle:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

PsaltyDS

Try OP example with

if $DataOut = "" then MsgBox(0, "RegRead", "Error " & $ErrValue)

if $DataOut == "" then MsgBox(0, "RegRead", "Error " & $ErrValue)

or your example

$Test1 = 0 = ""

ConsoleWrite('0 = "": ' & $Test1 & @LF)

$Test1 = 0 == ""

ConsoleWrite('0 == "": ' & $Test1 & @LF)

For "=" compare a zero and empty string is the same. Some time ago I had problems with this in one of my scripts too, until noticed it.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

PsaltyDS

Try OP example with

if $DataOut = "" then MsgBox(0, "RegRead", "Error " & $ErrValue)

if $DataOut == "" then MsgBox(0, "RegRead", "Error " & $ErrValue)

or your example

$Test1 = 0 = ""

ConsoleWrite('0 = "": ' & $Test1 & @LF)

$Test1 = 0 == ""

ConsoleWrite('0 == "": ' & $Test1 & @LF)

For "=" compare a zero and empty string is the same. Some time ago I had problems with this in one of my scripts too, until noticed it.

I see what you are saying but I think you misinterpret the reason for the result. Try:

$Test1 = 0 = "xyz"
ConsoleWrite('0 = "xyz": ' & $Test1 & @LF)
$Test1 = 0 == ""
ConsoleWrite('0 == "": ' & $Test1 & @LF)
$Test1 = 0 = 000
ConsoleWrite('0 = 000: ' & $Test1 & @LF)
$Test1 = 0 == "000"
ConsoleWrite('0 == 000: ' & $Test1 & @LF)

Any non-numeral string evaluates to 0 for numeric comparisons, which is why "" and "xyz" are equal to 0 in a numeric compare.

The operator '==' forces a string comparison, which is why 0 == 000 is false, but 0 = 000 is true. Don't force the string compare unless you mean it!

:whistle:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Which is exactly the case if someone is comparing something to ""

That's all I'm saying.

I hear 'ya, it's just that the original value being tested is a number (REG_DWORD), not a string. So it should be tested and compared numerically against numbers, not against strings...

Const $cRegKey = "HKLM\Software\Testing"
Const $cRegVal = "TestValue"
Const $cRegType = "REG_DWORD"
$DataIn = 0

RegWrite($cRegKey, $cRegVal, $cRegType, $DataIn)
If @error Then
    MsgBox(16, "Error", "Error in RegWrite, @error = " & @error)
    Exit
EndIf

$DataOut = RegRead($cRegKey, $cRegVal)
Select
    Case @error
        MsgBox(16, "Error", "Error in RegRead, @error = " & @error)
    Case IsString($DataOut)
        MsgBox(64, "RegRead", "Read REG_DWORD as a string: " & $DataOut)
    Case IsNumber($DataOut)
        MsgBox(64, "RegRead", "Read REG_DWORD as a number: " & $DataOut)
    Case Else
        MsgBox(16, "Error", "Did not recognize REG_DWORD read as a string or number!")
EndSelect

:whistle:

Edit: Keyboard made a spelling error... gotta get a new keyboard...

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...