我的要求是:当我运行NSIS安装程序时,首先它应该检查用户是否具有管理用户权限,如果用户具有权限,那么在安装时,应用程序应该在Windows注册表中写入路径和值 .
要检查管理权限,我在下面写了下面的代码片段
.onInit
RequestExecutionLevel admin
Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin"
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${Else}
MessageBox MB_OK "User is having Administrative Permissions"
${EndIf}
FunctionEnd
如果用户具有权限,我编写了以下代码来设置Windows注册表项 .
;--------------------------------
; The stuff to install
Section "RegistryTest (required)"
SectionIn RO
; Set output path to the installation directory. Here is the path C:\Program Files\RegistryTest
SetOutPath $INSTDIR
; Write the installation path into the registry
;Adding Registry entries under "Dialog"
WriteRegDWORD HKLM SOFTWARE\RegistryTest\Dialog "AppDataCollectionDlg" "1"
WriteRegStr HKLM SOFTWARE\RegistryTest\Dialog "ReplaceBatteryWebPage" "http://www.APC.com/tools/upgrade_selector/index.cfm?Localize=true"
WriteRegStr HKLM SOFTWARE\RegistryTest\Dialog "UpgradeUpsUrl" "http://www.APC.com/tools/upgrade_selector/index.cfm?Localize=true"
;Adding Registry entries under "EventLogging"
WriteRegStr HKLM SOFTWARE\RegistryTest\EventLogging "ImagePath" "$INSTDIR\eventlog.dat"
;Adding Registry entries under "Notifications"
WriteRegDWORD HKLM SOFTWARE\RegistryTest\Notification "Notification Sounds Enabled" "1"
;Adding Registry entries under "POT"
WriteRegDWORD HKLM SOFTWARE\RegistryTest\POT "Enabled" "0"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "HttpClient" "PotData.exe"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "HttpPacketVer" "1"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "HttpServer" "updates.apc.com"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "HttpServerUrl" "/pcpe/pot/potstatus.cfm"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "ProductCode" "3"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "ProductVer" "301"
WriteRegStr HKLM SOFTWARE\RegistryTest\POT "ZipCode" ""
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "DisplayName" "RegistryTest"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
Below are clarifications I am having:
我写的管理权限条件是否适用于所有操作系统,还是有任何特定的操作系统?
我编写的代码是首先检查权限然后将其写入注册表 . 这是正确的方法吗?如果需要更改,请建议 .
我在这里发现了一个观察结果:
我的用户是Admin用户,如果我们使用“HKLM”Windows注册表,那么当正常运行“NSIS”安装程序或以管理员身份运行时,它会将其视为管理用户并转到其他条件并将消息显示为“用户具有管理权限“
如果我使用“HKCU”Windows注册表,那么当正常运行“NSIS”安装程序时,它将进入“If”状态并显示“需要管理员权限!” . 如果我以管理员身份运行“NSIS”,它将其视为管理用户并转到其他条件并将消息显示为“用户具有管理权限” .
在这里,为什么“HKLM”和“HKCU”的情况会有所不同?是因为“HKLM”将拥有admin privis而“HKCU”将没有权限 .
在卸载部分,我不是直接使用uninstall.exe卸载Windows注册表中的每个部分 . 但卸载它时,将从windows注册中删除所有条目 . 那么,这里是不是需要卸载每个注册表项?