Jump to content

Reading Registry Stringvalues Woes


Recommended Posts

$c = "9999.exe"

;next two lines are all one line...

$d = RegRead("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue")

If $c <> $d Then

;next two lines are all one line...

RegWrite("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue", "REG_SZ", $c)

EndIf

The If $c <> $d Then... doesn't work if the registry key doesn't exisit. If I manually create the key with or without a value then it works fine.

According to the AutoIT3 Helpfile, RegRead() will return the value of the key if no errors are encountered. RegRead() errors will return either 1, -1, or 2.

So regardless of the value of $d, it will not be <> to $c, therefore, the registry key should be created and the value of $c written.

What am I missing?

Link to post
Share on other sites

EDIT: Make sure you are using the newest AutoIt version.

It *might* be a number-to-string conversion problem

RegRead Failure: Returns numeric 1 and sets the @error flag:

Try this:

If String($c) <> String($d)

Another Edit: Why not just write the key without reading it?

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to post
Share on other sites

My guess is that the variable doesn't get assigned anything if it is not there. So I would change to this.

$d = "NullValue"
$c = "9999.exe"
;next two lines are all one line...
$d = RegRead("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue")
If $c <> $d Then
;next two lines are all one line...
RegWrite("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue", "REG_SZ", $c)
EndIf

Assign a start value for $d that will never occur and then if there is no value assigned then your comparison is ok.

Also you can do

$c = "9999.exe"
;next two lines are all one line...
$d = RegRead("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue")
If @Error = 1 OR $c <> $d Then
;next two lines are all one line...
RegWrite("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue", "REG_SZ", $c)
EndIf

If the registry key doesn't exist it still writes $c to the registry.

Hope this helps

red

Link to post
Share on other sites

$d = "as12" ...nope

If @Error = 1 OR $c <> $d Then...nope

If $c <> String($d)...nope

None of these thre will work. I get the exact same error each time.

$d = RegRead("HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "MyNewStringValue")

Error: Unable to read/write the requested registry key.

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...