RegSearch: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 105: Line 105:




<b>Example (Search with banner - "[http://nsis.sourceforge.net/wiki/Banner_with_Cancel_button nxs]" plugin required):</b>
<b>Example (Search with banner - [http://nsis.sourceforge.net/wiki/Banner_with_Cancel_button nxs] plugin required):</b>
<highlight-nsis>
<highlight-nsis>
Section
Section

Revision as of 15:08, 4 July 2005

Author: Instructor (talk, contrib)


The Function Description

____________________________________________________________________________
 
                              RegSearch v1.0d
____________________________________________________________________________
 
 
Search in registry (HKCU and HKLM only). Required NSIS 2.06 or above.
 
 
Syntax:
${RegSearch} "[path]" "[Options]" "Function"
 
"[path]"      ;  Registry path (rootkey\path)
              ;
"[Options]"   ; /F=[VSK|VS|V|K|D]
              ;     /F=VSK    - Search Values, Strings and Keys (default)
              ;     /F=VS     - Search Values and Strings
              ;     /F=V      - Search Values only
              ;     /F=K      - Search Keys only
              ;     /F=D      - Search Default Values only
              ; /N=[name]
              ;     /N=          - Search for all (default)
              ;     /N=version   - Search for "version"
              ; /G=[1|0]
              ;     /G=1     - Search with subkeys (default)
              ;     /G=0     - Search without subkeys
              ; /B=[0|1]
              ;     /B=0     - Banner isn't used (default)
              ;     /B=1     - Banner is used. Callback when function
              ;                start to search in new key
"Function"    ; Callback function then found
 
Function "Function"
	; $R9    "path"
	; $R8    "key"
	; $R7    "value"
	; $R6    "string"
	; $R5    "type"   [KEY|STR|DWORD|BIN]
 
	; $R0-$R4  are not used (save data in them).
	; ...
 
 
	Push $var    ; If $var="StopRegSearch" Then exit from function
FunctionEnd
 
 
Note:
- Error flag if syntax error
- BIN type string is always empty
- HKLM=HKEY_LOCAL_MACHINE and HKCU=HKEY_CURRENT_USER


Example (Search for Value "Content Type"):

Section
	${RegSearch} "HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES" \
				"/F=V /N=Content Type" "Example1"
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd
 
Function Example1
	MessageBox MB_OKCANCEL '$$R9    "path"   =[$R9]$\n\
				$$R8    "key"    =[$R8]$\n\
				$$R7    "value" =[$R7]$\n\
				$$R6    "string" =[$R6]$\n\
				$$R5    "type"   =[$R5]' IDOK +2
	StrCpy $0 StopRegSearch
 
	Push $0
FunctionEnd


Example (Write founded in text file):

Section
	GetTempFileName $R1
	FileOpen $R0 $R1 w
	${RegSearch} "HKEY_CURRENT_USER\Control Panel" "/F=VSK" "Example2"
	FileClose $R0
 
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	Exec '"notepad.exe" "$R1"'
SectionEnd
 
Function Example2
	StrCmp $R5 'KEY' 0 +3
	FileWrite $R0 '$R5:"$R9\$R8"$\r$\n'
	goto +2
	FileWrite $R0 '$R5:"$R9" "$R7"="$R6"$\r$\n'
 
	Push $0
FunctionEnd


Example (Search with banner - nxs plugin required):

Section
	nxs::Show /NOUNLOAD `$(^Name) Setup`\
		/top `Setup searching something$\nPlease wait... If you can...`\
		/h 1 /can 1 /end
	${RegSearch} "HKLM\System" "/F=V /B=1" "Example3"
	nxs::Destroy

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd
 
Function Example3
	StrCmp $R0 $R9 abortcheck
	StrCpy $R0 $R9
	nxs::Update /NOUNLOAD /sub "$R9" /pos 78 /end
 
	abortcheck:
	nxs::HasUserAborted /NOUNLOAD
	Pop $0
	StrCmp $0 1 0 +2
	StrCpy $0 StopRegSearch
 
	StrCmp $R5 '' end
	;...
 
	end:
	Push $0
FunctionEnd

The Function Code

Function RegSearch
	!define RegSearch `!insertmacro RegSearchCall`
 
	!macro RegSearchCall _PATH _OPTIONS _FUNC
		Push $0
		Push `${_PATH}`
		Push `${_OPTIONS}`
		GetFunctionAddress $0 `${_FUNC}`
		Push `$0`
		Call RegSearch
		Pop $0
	!macroend
 
	Exch $2
	Exch
	Exch $1
	Exch
	Exch 2
	Exch $0
	Exch 2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $8
	Push $R5
	Push $R6
	Push $R7
	Push $R8
	Push $R9
	ClearErrors
 
	StrCpy $3 ''
	StrCpy $4 ''
	StrCpy $5 ''
	StrCpy $6 ''
	StrCpy $7 0
 
	StrCpy $R9 $0 1 -1
	StrCmp $R9 '\' 0 +3
	StrCpy $0 $0 -1
	goto -3
	StrCpy $R8 0
	StrCpy $R9 $0 1 $R8
	StrCmp $R9 '' +4
	StrCmp $R9 '\' +3
	IntOp $R8 $R8 + 1
	goto -4
	StrCpy $R9 $0 $R8
	IntOp $R8 $R8 + 1
	StrCpy $0 $0 '' $R8
 
	StrCmp $R9 'HKLM' +2
	StrCmp $R9 'HKEY_LOCAL_MACHINE' 0 +3
	SetShellVarContext all
	goto ifkeyexists
	StrCmp $R9 'HKCU' +2
	StrCmp $R9 'HKEY_CURRENT_USER' 0 error
	SetShellVarContext current
 
	ifkeyexists:
;	ReadRegStr $R9 SHCTX $0 ''
;	IfErrors error
 
	option:
	StrCpy $R9 $1 1
	StrCpy $1 $1 '' 1
	StrCmp $R9 ' ' -2
	StrCmp $R9 '' default
	StrCmp $R9 '/' 0 -4
	StrCpy $R7 -1
	IntOp $R7 $R7 + 1
	StrCpy $R9 $1 1 $R7
	StrCmp $R9 '' +2
	StrCmp $R9 '/' 0 -3
	StrCpy $R8 $1 $R7
	StrCpy $R8 $R8 '' 2
	StrCpy $R9 $R8 '' -1
	StrCmp $R9 ' ' 0 +3
	StrCpy $R8 $R8 -1
	goto -3
	StrCpy $R9 $1 2
	StrCpy $1 $1 '' $R7
 
	StrCmp $R9 'F=' 0 mask
	StrCpy $3 $R8
	StrCmp $3 '' +6
	StrCmp $3 'VSK' +5
	StrCmp $3 'VS' +4
	StrCmp $3 'V' +3
	StrCmp $3 'K' +2
	StrCmp $3 'D' 0 error
	goto option
 
	mask:
	StrCmp $R9 'N=' 0 gotosubdir
	StrCpy $4 $R8
	goto option
 
	gotosubdir:
	StrCmp $R9 'G=' 0 banner
	StrCpy $5 $R8
	StrCmp $5 '' +3
	StrCmp $5 '1' +2
	StrCmp $5 '0' 0 error
	goto option
 
	banner:
	StrCmp $R9 'B=' 0 error
	StrCpy $6 $R8
	StrCmp $6 '' +3
	StrCmp $6 '1' +2
	StrCmp $6 '0' 0 error
	goto option
 
	default:
	StrCmp $3 '' 0 +2
	StrCpy $3 'VSK'
	StrCmp $5 '' 0 +2
	StrCpy $5 '1'
	StrCmp $6 '' 0 +2
	StrCpy $6 '0'
 
	StrCpy $7 1
	Push $0
	SetDetailsPrint textonly
 
	popkey:
	StrCmp $7 '0' end
	IntOp $7 $7 - 1
	Pop $R9
 
	StrCpy $R5 ''
	StrCpy $R6 ''
	StrCpy $R7 ''
	StrCpy $R8 ''
	StrCmp $6 '0' +3
	GetLabelAddress $1 readdefault
	goto call
	DetailPrint 'Search in: $R9'
 
	readdefault:
	StrCmp $3 'V' enumvalue
	StrCmp $3 'K' enumkey
	ReadRegStr $R6 SHCTX $R9 ''
	StrCpy $R5 STR
	IfErrors 0 +5
	ReadRegDWORD $R6 SHCTX $R9 ''
	StrCpy $R5 DWORD
	IfErrors 0 +2
	StrCpy $R5 BIN
	StrCmp $3 'D' 0 +3
	GetLabelAddress $1 enumkey
	goto +2
	GetLabelAddress $1 enumvalue
	StrCmp $R6 '' $1
	StrCmp $4 '' call
	StrCmp $4 $R6 call $1
 
	enumvalue:
	StrCpy $0 0
	nextvalue:
	EnumRegValue $R7 SHCTX $R9 $0
	ClearErrors
	StrCmp $R7 '' enumkey
	IntOp $0 $0 + 1
	StrCmp $4 '' +3
	StrCmp $4 $R7 +2
	StrCmp $3 'V' nextvalue
 
	ReadRegStr $R6 SHCTX $R9 $R7
	StrCpy $R5 STR
	IfErrors 0 +5
	ReadRegDWORD $R6 SHCTX $R9 $R7
	StrCpy $R5 DWORD
	IfErrors 0 +2
	StrCpy $R5 BIN
	StrCmp $3 'V' +4
	StrCmp $4 '' +3
	StrCmp $4 $R7 +2
	StrCmp $4 $R6 0 nextvalue
	GetLabelAddress $1 nextvalue
	goto call
 
	enumkey:
	StrCmp $5 '0' end
	StrCpy $R5 KEY
	StrCpy $R6 ''
	StrCpy $R7 ''
	StrCpy $0 0
	nextkey:
	EnumRegKey $R8 SHCTX $R9 $0
	ClearErrors
	StrCmp $R8 '' popkey
	IntOp $0 $0 + 1
	IntOp $7 $7 + 1
	StrCmp $R9 '' 0 +3
	Push $R8
	goto +2
	Push '$R9\$R8'
	StrCmp $3 'VSK' +2
	StrCmp $3 'K' 0 nextkey
	StrCmp $4 '' +2
	StrCmp $R8 $4 0 nextkey
	GetLabelAddress $1 nextkey
 
	call:
	Push $0
	Push $1
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $R5
	Push $R9
	Call $2
	Pop $8
	Pop $R9
	Pop $R5
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	IfErrors error
 
	StrCmp $8 'StopRegSearch' clearstack
	StrCpy $R6 ''
	StrCpy $R7 ''
	StrCpy $R8 ''
	goto $1
 
	error:
	SetErrors
 
	clearstack:
	StrCmp $7 0 end
	IntOp $7 $7 - 1
	Pop $R9
	goto clearstack
 
	end:
	SetShellVarContext current
	SetDetailsPrint both
	Pop $R9
	Pop $R8
	Pop $R7
	Pop $R6
	Pop $R5
	Pop $8
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd