[Closed] Problem with registry methods
Hello!
I’m having some trouble running a script with registry actions more than once.
I’m using registry.openKey, registry.createKey, registry.getValueName, registry.queryInfoKey and registry.queryValue.
The script works flawlessly, but the second time I run it, it breaks. I first thought I did some sort of mistake, but I found out that it will work, but only after restarting 3dsMax.
So basically I can run the script as many times as I want, but only one time per max session. :shrug:
This is the error I’m getting:
-- Type error: HKey requires HKey, got: undefined
It doesn’t make any sense to me, because the HKey is clearly defined a line above it, and again, works flawlessly the first time you run it. The second time you run it though, the HKey variable just returns undefined.
Do you guys have any idea what’s going on? Is there a way to maybe delete the variables from memory or something like that? So that the script runs on a clean slate every time?
oh i smell scope issues
mind posting your code?
i allways use them functions… little old and messy but they get it done
:
fn gIs Category Key File:"Frowser" =
(
local type, val, key1
registry.createKey HKEY_CURRENT_USER ("Software\\YourApp\\"+File as string + "\\" + Category as string) accessRights:#all newKeyCreated:&newKeyCreated key:&key1
registry.queryValue key1 (Key as string) type:&type value:&val
val
)--END gIs FN
fn sIs Category Key Val File:"Frowser" del:false =
(
try(
local newKeyCreated , key1
registry.createKey HKEY_CURRENT_USER ("Software\\YourApp\\"+File as string + "\\" + Category as string) accessRights:#all newKeyCreated:&newKeyCreated key:&key1
if not del then
(
registry.setvalue key1 (Key as string) #REG_SZ (Val as string)
)
else
(
registry.deleteKey key1
)
)catch( print (registry.getLastError()) )
)--END sIs FN
hey Insanto, actually I was just checking the examples you have on your site.
I think I found the problem… This is what I currently have:
try
(
registry.openKey HKEY_LOCAL_MACHINE "SOFTWARE" accessRights:#readOnly key:&key1
registry.openKey key1 "Classes" accessRights:#readOnly key:&key2
registry.openKey key2 "3dsmax" accessRights:#readOnly key:&key3
registry.openKey key3 "DefaultIcon" accessRights:#readOnly key:&key4
default_max = registry.queryInfoKey key4 numValues:&numValues
)
catch
(
print "NO DEFAULTICON ENTRY"
default_max == false
)
if default_max == true then
(
registry.getValueName key4 1 name:&theValName
registry.queryValue key4 theValName type:&theType value:&theValue
print theValue
)
else
(
print "do this"
)
I think the try method is somehow messing things up. What I would like to do, is for the script to check if those key’s actually exist in the registry. If not “default_max == false”, if it is found it’s “true”.
I think it’s the wrong approach, is it?
How can I check whether those keys exist or not, before doing one thing or the other?
This on the other hand works perfectly:
registry.openKey HKEY_LOCAL_MACHINE "SOFTWARE" accessRights:#readOnly key:&key1
registry.openKey key1 "Classes" accessRights:#readOnly key:&key2
registry.openKey key2 "3dsmax" accessRights:#readOnly key:&key3
registry.openKey key3 "DefaultIcon" accessRights:#readOnly key:&key4
default_max = registry.queryInfoKey key4 numValues:&numValues
registry.getValueName key4 1 name:&theValName
registry.queryValue key4 theValName type:&theType value:&theValue
print theValue
But of course, it will break if the key is not there.
== false ?
== is a for boolean comparisons, if you want to assign the false value to that var you only use one =
you expect the print of “do this” after restart wich isnt possible as you dont assign anything to the var… did you declare the var with default val higher up in the scope?
eg:
default_max = false
registry.openKey HKEY_LOCAL_MACHINE "SOFTWARE" accessRights:#readOnly key:&key1
registry.openKey key1 "Classes" accessRights:#readOnly key:&key2
registry.openKey key2 "3dsmax" accessRights:#readOnly key:&key3
registry.openKey key3 "DefaultIcon" accessRights:#readOnly key:&key4
default_max = registry.queryInfoKey key4 numValues:&numValues
default_max = if classOf default_max == string then execute default_max else false
if default_max == true then
(
registry.getValueName key4 1 name:&theValName
registry.queryValue key4 theValName type:&theType value:&theValue
print theValue
)
else
(
print "do this"
)
also… afair the reg methods retrieve vals in string format so to eval you gotta execute you val to the var you wish to use for if statements
didnt test the code but might work
If a key does not exist, the openkey method returns false, there is no need to use try/catch.
D’oh! Of course… That’s embarrassing! Thanks guys, problem solved…
Move along… nothing to see here… hahah