Sign in to follow this  
Followers 0
PerryRaptor

Reading Registry Stringvalues Woes

8 posts in this topic

$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?

Share this post


Link to post
Share on other sites



RegRead sets @error, you need to check that, not the return value.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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!

Share this post


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

Share this post


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.

Share this post


Link to post
Share on other sites

Wait, is this a message box popping up with an error or something?

Share this post


Link to post
Share on other sites

Yes, AutoIT Error with the line

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

Share this post


Link to post
Share on other sites

Yes, it is case sensitive "HKEY_LOCAL_MACHINE" must be in all caps...Never would have figured that one out by myself!!!

Share this post


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
Sign in to follow this  
Followers 0