例子:检查某注册表键是否存在

优质
小牛编辑
184浏览
2023-12-01
首先定义注册表主键:
!define HKEY_CLASSES_ROOT 0x80000000
!define HKEY_CURRENT_USER 0x80000001
!define HKEY_LOCAL_MACHINE 0x80000002
!define HKEY_USERS 0x80000003


利用RegOpenKey这个API的返回值进行判断:
如果$R1=0,则该注册表键存在;
$R1=2,表示该注册表键不存在。
其它返回值可能是运行错误。

引用 OpenReg.nsi 的内容:

; OpenReg.nsi
; 检查某注册表键是否存在的NSIS例子
; 编写:zhfi 

;--------------------------------
;定义注册表主键
!define HKEY_CLASSES_ROOT           0x80000000
!define HKEY_CURRENT_USER           0x80000001
!define HKEY_LOCAL_MACHINE          0x80000002
!define HKEY_USERS                  0x80000003

OutFile OpenReg.exe

XPStyle on

!include LogicLib.nsh

;--------------------------------
Name OpenReg

Section Nil
SectionEnd

Function .onInit
;为键的句柄创建一个缓存
System::Call "*(i 0) i .R0"
;将要检测的键放入内存中
Push "SOFTWARE\TENCENT\QQPinYin"
;调用API进行检查,返回值在$R1中
system::call 'Advapi32::RegOpenKey(i ${HKEY_LOCAL_MACHINE}, t s, i R0) .iR1'
;关闭该键的句柄
system::call 'Advapi32::RegCloseKey(i R0)'
;释放内存
system::free
;返回值:
;0代表键存在
;2代表键不存在
;其它值表示出错
${If} $R1 == 0
Messagebox mb_ok "Key Exists!"
${ElseIf} $R1 == 2
Messagebox mb_ok "Key doesn't Exists!"
${Else}
Messagebox mb_ok "Error!"
${EndIf}
Pop $R1
Pop $R0
Quit
FunctionEnd