Check for a Registry Key: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author and download links, and changed format of some pages.)
(added simpler logiclib version)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{PageAuthor|Vytautas}}
== Description ==
== Description ==
Include this macro to check to see if a specified registry key exists.  After using this macro the stack contains 0 if the key was not present and 1 if it was.
Include this macro to check to see if a specified registry key exists.  After using this macro the stack contains 0 if the key was not present and 1 if it was.


'''Note:''' You can also check the error flag after reading the default value of the registry key to know whether the key exits, that is easier in most situations.
'''[UPDATE: 2004-04-07]'''
 
; [UPDATE: 2004-04-07]
: Fixed another typo.
: Fixed another typo.
; [UPDATE: 2003-11-14]
'''[UPDATE: 2003-11-14]'''
: Fixed a typo in latest version.
: Fixed a typo in latest version.
; [UPDATE: 2003-11-12]
'''[UPDATE: 2003-11-12]'''
: Added Indexed labels to the macro to enable easier modification if needed.
: Added Indexed labels to the macro to enable easier modification if needed.


Line 17: Line 17:
;$R0 contains 0 (not present) or 1 (present)
;$R0 contains 0 (not present) or 1 (present)
</highlight-nsis>
</highlight-nsis>
== The Macro w/ LogicLib ==
<highlight-nsis>!include LogicLib.nsh
!macro IfKeyExists ROOT MAIN_KEY KEY
  Push $R0
  Push $R1
  Push $R2
 
  # XXX bug if ${ROOT}, ${MAIN_KEY} or ${KEY} use $R0 or $R1


  StrCpy $R1 "0" # loop index
  StrCpy $R2 "0" # not found
 
  ${Do}
    EnumRegKey $R0 ${ROOT} "${MAIN_KEY}" "$R1"
    ${If} $R0 == "${KEY}"
      StrCpy $R2 "1" # found
      ${Break}
    ${EndIf}
    IntOp $R1 $R1 + 1
  ${LoopWhile} $R0 != ""
 
  ClearErrors
 
  Exch 2
  Pop $R0
  Pop $R1
  Exch $R2
!macroend</highlight-nsis>


== The Macro ==
== The Macro ==
Line 56: Line 83:
Vytautas
Vytautas


Page author: [[User:Vytautas|Vytautas]]
[[Category:INI, CSV & Registry Functions]]

Latest revision as of 18:05, 24 November 2009

Author: Vytautas (talk, contrib)


Description

Include this macro to check to see if a specified registry key exists. After using this macro the stack contains 0 if the key was not present and 1 if it was.

[UPDATE: 2004-04-07]

Fixed another typo.

[UPDATE: 2003-11-14]

Fixed a typo in latest version.

[UPDATE: 2003-11-12]

Added Indexed labels to the macro to enable easier modification if needed.

Usage

!insertmacro IfKeyExists "ROOT" "KeyToCheckIn" "KeyToCheck"
Pop $R0
;$R0 contains 0 (not present) or 1 (present)

The Macro w/ LogicLib

!include LogicLib.nsh
!macro IfKeyExists ROOT MAIN_KEY KEY
  Push $R0
  Push $R1
  Push $R2
 
  # XXX bug if ${ROOT}, ${MAIN_KEY} or ${KEY} use $R0 or $R1
 
  StrCpy $R1 "0" # loop index
  StrCpy $R2 "0" # not found
 
  ${Do}
    EnumRegKey $R0 ${ROOT} "${MAIN_KEY}" "$R1"
    ${If} $R0 == "${KEY}"
      StrCpy $R2 "1" # found
      ${Break}
    ${EndIf}
    IntOp $R1 $R1 + 1
  ${LoopWhile} $R0 != ""
 
  ClearErrors
 
  Exch 2
  Pop $R0
  Pop $R1
  Exch $R2
!macroend

The Macro

!macro IfKeyExists ROOT MAIN_KEY KEY
push $R0
push $R1
 
!define Index 'Line${__LINE__}'
 
StrCpy $R1 "0"
 
"${Index}-Loop:"
; Check for Key
EnumRegKey $R0 ${ROOT} "${MAIN_KEY}" "$R1"
StrCmp $R0 "" "${Index}-False"
  IntOp $R1 $R1 + 1
  StrCmp $R0 "${KEY}" "${Index}-True" "${Index}-Loop"
 
"${Index}-True:"
;Return 1 if found
push "1"
goto "${Index}-End"
 
"${Index}-False:"
;Return 0 if not found
push "0"
goto "${Index}-End"
 
"${Index}-End:"
!undef Index
exch 2
pop $R0
pop $R1
!macroend

This page was created due to this thread.

Vytautas