Archive: Using variable for registry root_key in reg methods


Using variable for registry root_key in reg methods
I'm using registry functions such as EnumRegKey and, depending on user access rights, reading from HKLM or HKCU. I'm not finding a way to pass the root key (HKLM or HKCU) as a variable instead of hard coded. Passing root_key as a variable would allow me to reuse code instead of having to duplicate. Something like:

IntOp $ROOTKEY 0 + HKLM
EnumRegKey $MYKEY $ROOTKEY Software\MyHive
IfErrors 0 foundit
IntOp $ROOTKEY 0 + HKCU
EnumRegKey $MYKEY $ROOTKEY Software\MyHive
IfErrors 0 foundit
; MyHive not found
Goto done

foundit:
; MyHive found in $ROOTKEY, operate from that root

done:


HKLM and HKCU currently can only be set on compile-time, therefore you have to do it like this:


ClearErrors
StrCpy $ROOTKEY HKLM
EnumRegKey $MYKEY HKLM Software\MyHive
IfErrors 0 foundit
StrCpy $ROOTKEY HKCU
EnumRegKey $MYKEY HKCU Software\MyHive
IfErrors 0 foundit
; MyHive not found

Goto done
foundit:
; MyHive found in $ROOTKEY, operate from that root

done:


Remember to ClearErrors first!

-Stu

To change HKLM and HKCU at runtime use SHCTX (NSIS 2.06).

SetShellVarContext all
EnumRegKey $MYKEY SHCTX Software\MyHive
IfErrors 0 foundit
SetShellVarContext current
EnumRegKey $MYKEY SHCTX Software\MyHive
IfErrors 0 foundit
; MyHive not found

Goto done
foundit:
; MyHive found in $ROOTKEY, operate from that root

done:

no joy
I'll look into SHCTX.

Thanx for the ClearErrors reminder ;o)

cl

*edit: SHCTX does exactly what I'm lookin for. Thanx!