Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Показать сообщение отдельно

Старожил


Сообщения: 352
Благодарности: 193

Профиль | Отправить PM | Цитировать


С помощью функций Win API возможна более корректная проверка ключей на не_существует/нет_доступа/другие_ошибки
пример
Код: Выделить весь код
ShowInstDetails show
InstallColors /windows
SetFont "Consolas" 10
AutoCloseWindow false
SetOverwrite on
RequestExecutionLevel user

!include LogicLib.nsh
!include RegistryFunc.nsh
!include SystemFunc.nsh

Section 
  ${GetRegKeyStatus} $R0 HKLM 'Software\Microsoft' ; exist
  ${GetSystemErrorMessage} $R1 $R0
  DetailPrint "Soft\MS: $R0: $R1"
  
  ${GetRegKeyStatus} $R0 HKLM 'qwertyuiop' ; not exist
  ${GetSystemErrorMessage} $R1 $R0
  DetailPrint "qwertyu: $R0: $R1"
  
  ${GetRegKeyStatus} $R0 HKLM 'SAM\SAM' ; access denied
  ${GetSystemErrorMessage} $R1 $R0
  DetailPrint "SAM\SAM: $R0: $R1"
SectionEnd

Section
  ${If} HKLM RegKeyExists 'Software\Microsoft'
    DetailPrint "Soft\MS: exists"
  ${EndIf}
  ${If} HKLM RegKeyExists 'qwertyuiop'
    DetailPrint "qwertyu: exists"
  ${EndIf}
  ${If} HKLM RegKeyExists 'SAM\SAM'
    DetailPrint "SAM\SAM: exists"
  ${EndIf}

  ${If} HKLM RegKeyNotExists 'Software\Microsoft'
    DetailPrint "Soft\MS: not exists"
  ${EndIf}
  ${If} HKLM RegKeyNotExists 'qwertyuiop'
    DetailPrint "qwertyu: not exists"
  ${EndIf}
  ${If} HKLM RegKeyNotExists 'SAM\SAM'
    DetailPrint "SAM\SAM: not exists"
  ${EndIf}

  ${If} HKLM RegKeyAccessDenied 'Software\Microsoft'
    DetailPrint "Soft\MS: access denied"
  ${EndIf}
  ${If} HKLM RegKeyAccessDenied 'qwertyuiop'
    DetailPrint "qwertyu: access denied"
  ${EndIf}
  ${If} HKLM RegKeyAccessDenied 'SAM\SAM'
    DetailPrint "SAM\SAM: access denied"
  ${EndIf}
SectionEnd

RegistryFunc.nsh
Код: Выделить весь код
!ifndef RegistryFunc.nsh
!define RegistryFunc.nsh

!include LogicLib.nsh
!include WinCore.nsh

!define KEY_QUERY_VALUE          0x0001
!define KEY_ENUMERATE_SUB_KEYS   0x0008

!macro Push_root_key root_key
  !if      ${root_key} == HKEY_CLASSES_ROOT
  Push ${HKEY_CLASSES_ROOT}
  !else if ${root_key} == HKCR
  Push ${HKEY_CLASSES_ROOT}
  !else if ${root_key} == HKEY_CURRENT_USER
  Push ${HKEY_CURRENT_USER}
  !else if ${root_key} == HKCU
  Push ${HKEY_CURRENT_USER}
  !else if ${root_key} == HKEY_LOCAL_MACHINE
  Push ${HKEY_LOCAL_MACHINE}
  !else if ${root_key} == HKLM
  Push ${HKEY_LOCAL_MACHINE}
  !else if ${root_key} == HKEY_USERS
  Push ${HKEY_USERS}
  !else if ${root_key} == HKU
  Push ${HKEY_USERS}
  !else if ${root_key} == HKEY_PERFORMANCE_DATA
  Push ${HKEY_PERFORMANCE_DATA}
  !else if ${root_key} == HKEY_CURRENT_CONFIG
  Push ${HKEY_CURRENT_CONFIG}
  !else if ${root_key} == HKCC
  Push ${HKEY_CURRENT_CONFIG}
  !else if ${root_key} == HKEY_DYN_DATA
  Push ${HKEY_DYN_DATA}
  !else
  Push '${root_key}'
  !endif
!macroend

!define GetRegKeyStatus `!insertmacro GetRegKeyStatus `
!macro GetRegKeyStatus result root_key sub_key
  Push $0
  Push '${sub_key}'
  !insertmacro Push_root_key ${root_key}
  System::Call "Advapi32::RegOpenKeyEx(is, ts, i0, i${KEY_QUERY_VALUE}|${KEY_ENUMERATE_SUB_KEYS}, *p.r0) i.s"
  StrCmp $0 0 +2 
  System::Call "Advapi32::RegCloseKey(i r0) i"
  Exch
  Pop $0
  Pop ${result}
!macroend

!define RegKeyExists `RegKeyExists`
!macro _RegKeyExists _a _b _t _f
  !insertmacro _LOGICLIB_TEMP
  ClearErrors
  EnumRegKey $_LOGICLIB_TEMP ${_a} `${_b}` 1
  IfErrors `${_f}` `${_t}` 
!macroend

!define RegKeyNotExists `RegKeyNotExists`
!macro _RegKeyNotExists _a _b _t _f
  !insertmacro _LOGICLIB_TEMP
  ${GetRegKeyStatus} $_LOGICLIB_TEMP ${_a} '${_b}'
  StrCmp $_LOGICLIB_TEMP 2 `${_t}` `${_f}`
!macroend

!define RegKeyAccessDenied `RegKeyAccessDenied`
!macro _RegKeyAccessDenied _a _b _t _f
  !insertmacro _LOGICLIB_TEMP
  ${GetRegKeyStatus} $_LOGICLIB_TEMP ${_a} '${_b}'
  StrCmp $_LOGICLIB_TEMP 5 `${_t}` `${_f}`
!macroend

!endif

SystemFunc.nsh
Код: Выделить весь код
!ifndef SystemFunc.nsh
!define SystemFunc.nsh

!define GetSystemErrorMessage `!insertmacro GetSystemErrorMessage `
!macro GetSystemErrorMessage result error_code
  System::Call 'kernel32::GetSystemDefaultUILanguage()i.s'
  Push ${error_code}
  System::Call "kernel32::FormatMessage(i0x1300, in, is, is, *t.s, i${NSIS_MAX_STRLEN}, in)i" 
  Pop ${result}
!macroend

!endif

Отправлено: 01:02, 11-08-2023 | #2780