NSIS 常用操作代码
获取net版本
;获取.Net Framework版本支持
Function GetNetFrameworkVersion
Push $1
Push $0
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "2.0.50727.832"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.1.4322.573"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.0.3705.0"
StrCmp $0 1 KnowNetFrameworkVersion +1
StrCpy $1 "not .NetFramework"
KnowNetFrameworkVersion:
Pop $0
Exch $1
FunctionEnd
下载微软证书
; 微软证书下载回调
Function MicrosoftCertificatePackDownLoadCallBack
; 0-当前进度(百分比)
Pop $0
; 1-累计大小
Pop $1
; 2-已下载大小
Pop $2
; 3-下载速度
Pop $3
; 4-剩余时间
Pop $4
;更新包下载进度
; 当前进度
push $0
; 当前剩余时间
push $4
FunctionEnd
;下载微软证书
Function DownloadMicrosoftCertificate
GetFunctionAddress $R9 MicrosoftCertificatePackDownLoadCallBack
inetc::get "${MS_ROOT_CERT_DL_URL}" "$TEMP\${MS_ROOT_CERT_NAME}" $R9
; 读取值
Pop $1
; 写入值($1="ok"表示下载成功)
Push $1
FunctionEnd
安装微软证书
AddCertificateToStore
Exch $0
Push $1
Push $R0
System::Call "crypt32::CryptQueryObject(i ${CERT_QUERY_OBJECT_FILE}, w r0, \
i ${CERT_QUERY_CONTENT_FLAG_ALL}, i ${CERT_QUERY_FORMAT_FLAG_ALL}, \
i 0, i 0, i 0, i 0, i 0, i 0, *i .r0) i .R0"
${If} $R0 <> 0
System::Call "crypt32::CertOpenStore(i ${CERT_STORE_PROV_SYSTEM}, i 0, i 0, \
i ${CERT_STORE_OPEN_EXISTING_FLAG}|${CERT_SYSTEM_STORE_LOCAL_MACHINE}, \
w 'ROOT') i .r1"
${If} $1 <> 0
System::Call "crypt32::CertAddCertificateContextToStore(i r1, i r0, \
i ${CERT_STORE_ADD_ALWAYS}, i 0) i .R0"
System::Call "crypt32::CertFreeCertificateContext(i r0)"
${If} $R0 = 0
StrCpy $0 "Unable to add certificate to certificate store"
${Else}
StrCpy $0 "success"
${EndIf}
System::Call "crypt32::CertCloseStore(i r1, i 0)"
${Else}
System::Call "crypt32::CertFreeCertificateContext(i r0)"
StrCpy $0 "Unable to open certificate store"
${EndIf}
${Else}
StrCpy $0 "Unable to open certificate file"
${EndIf}
Pop $R0
Pop $1
Exch $0
FunctionEnd
; 安装微软证书
Function InstallMicrosoftCertificate
Push $TEMP\${MS_ROOT_CERT_NAME}
Call AddCertificateToStore
Pop $0
${If} $0 == success
; 安装完成,删除文件
Delete "$TEMP\${MS_ROOT_CERT_NAME}"
${EndIf}
; $0=success表示安装成功
Push $0
FunctionEnd
下载net安装包
; Net安装包下载回调
Function NetPackDownLoadCallBack
; 0-当前进度(百分比)
Pop $0
; 1-累计大小
Pop $1
; 2-已下载大小
Pop $2
; 3-下载速度
Pop $3
; 4-剩余时间
Pop $4
FunctionEnd
;下载 .NET Framework 4.0
Function DownloadNetFramework4
GetFunctionAddress $R9 NetPackDownLoadCallBack
inetc::get "${NET_PACK_DL_URL}" "$TEMP\${NET_PACK_NAME}" $R9
; 读取值
Pop $1
; 写入值($1="ok"表示下载成功)
Push $1
FunctionEnd
安装net安装包
; 安装net包
Function InstallDotNetPack
; 安装net包
ExecWait '$TEMP\${NET_PACK_NAME} /q /norestart /ChainingPackage FullX64Bootstrapper' $R1
; 安装成功(安装成功返回0 16386 文件损坏 返回当前版本号 文件不存在)
${If} $R1 == 0
; 安装完成,删除安装包
Delete "$TEMP\${NET_PACK_NAME}"
${EndIf}
; 返回值($R1=0表示安装成功)
Push $R1
FunctionEnd
Net环境检测
; 检查net环境
Function CheckNetCondition
; net版本验证及安装
;检测是否是需要的.NET Framework版本
Call GetNetFrameworkVersion
Pop $R1
; ${If} $R1 < '4.7.03062'
${If} $R1 < '4.8.03761'
; 下载微软证书
GetFunctionAddress $0 DownloadMicrosoftCertificate
; 等待结果
BgWorker::CallAndWait
; 弹出下载结果
Pop $R1
; 下载成功验证
${If} $R1 == "ok"
; 微软证书
GetFunctionAddress $0 InstallMicrosoftCertificate
BgWorker::CallAndWait
; 弹出安装结果
Pop $R2
; 安装结果验证
${If} $R2 != success
#微软证书安装完成
${Endif}
${EndIf}
; 下载net安装包
GetFunctionAddress $0 DownloadNetFramework4
; 等待结果
BgWorker::CallAndWait
; 弹出下载结果
Pop $R3
; 下载成功验证
${If} $R3 == "ok"
; 安装net包
GetFunctionAddress $0 InstallDotNetPack
BgWorker::CallAndWait
; 弹出安装结果
Pop $R4
; 安装结果验证
${If} $R4 == 0
#net包安装成功
${EndIf}
${EndIf}
${EndIf}
FunctionEnd
结束指定进程
#注:ShowMsgBox 可更换为MessageBox使用系统提示框提示
; 结束进程
; 返回0 表示结束成功 返回1 表示退出安装
Function KillProc
#此处检测当前是否有程序正在运行,如果正在运行,提示先卸载再安装
nsProcess::_FindProcess "${EXE_NAME}"
Pop $R0
#验证查询结果
${If} $R0 == 0
; 弹框提示
StrCpy $R8 "检测到 ${EXE_NAME} 正在运行。点击 “确定” 结束进程${EXE_NAME},继续安装。点击 “取消” 退出安装程序。"
StrCpy $R7 "1"
Call ShowMsgBox
Pop $0
; 结束进程
${If} $0 == 1
; 设置安装提示
nsNiuniuSkin::SetControlAttribute $hInstallDlg "progress_tip" "text" "正在安全的结束进程,请稍后..."
#结束进程
KillProcDLL::KillProc"${EXE_NAME}"
${Else}
#设置返回值
push 1
goto KillProcEnd
${EndIf}
#循环验证
${For} $R1 0 100
#等待100毫秒再查询结果
Sleep 100
#接收结果
nsProcess::_FindProcess "${EXE_NAME}"
#检测进程
Pop $R0
; 判断进程是否存在
${If} $R0 != 0
#设置返回值
push 0
; 查找进程结束
goto KillProcEnd
${EndIf}
${Next}
; 弹框提示
StrCpy $R8 "我们无法安全的结束正在运行的 ${EXE_NAME} 应用程序,请手动退出应用程序,再尝试安装!"
StrCpy $R7 "0"
Call ShowMsgBox
#设置返回值
push 1
KillProcEnd:
${EndIf}
FunctionEnd
创建桌面快捷方式
;创建桌面快捷方式
Function CreateDeskTopIco
#添加到桌面快捷方式的动作在此添加
SetShellVarContext all
CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${EXE_NAME}"
SetShellVarContext current
FunctionEnd
创建任务栏快捷方式
注:Win10下可能存在问题,系统机制原因
;创建任务栏快捷方式
Function CreateBarlnk
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
${if} $R0 >= 6.0
SetOutPath $INSTDIR
;注意这句与下一句是有先后顺序的(与ExecShell taskbarpin关联)
CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${EXE_NAME}"
;创建任务栏快捷方式(win10系统会失败,并且导致程序运行)
;ExecShell taskbarpin "$DESKTOP\${PRODUCT_NAME}.lnk"
${StdUtils.InvokeShellVerb} $0 "$INSTDIR" "${EXE_NAME}" ${StdUtils.Const.ShellVerb.PinToTaskbar}
${else}
CreateShortCut "$QUICKLAUNCH\${PRODUCT_NAME}.lnk" "$INSTDIR\${EXE_NAME}"
${Endif}
FunctionEnd
添加开机启动
注:应用软件如果需要管理员身份启动,开机可能无法正常启动
; 创建开机启动
; 备注:开机启动的项目不能为管理员身份启动,否则会启动不起来
Function CreateBootStart
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "${PRODUCT_NAME}" "$INSTDIR\${EXE_NAME}"
FunctionEnd
创建开始菜单
Function CreateAppShortcut
SetShellVarContext all
CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "$INSTDIR\${EXE_NAME}"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\卸载${PRODUCT_NAME}.lnk" "$INSTDIR\uninst.exe"
SetShellVarContext current
FunctionEnd
创建卸载信息
# 生成卸载入口
Function CreateUninstall
#写入注册信息
SetRegView 32
WriteRegStr HKLM "Software\${PRODUCT_PATHNAME}" "InstPath" "$INSTDIR"
; WriteUninstaller "$INSTDIR\uninst.exe"
# 添加卸载信息到控制面板
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}" "DisplayName" "${PRODUCT_NAME}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}" "UninstallString" "$INSTDIR\uninst.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}" "DisplayIcon" "$INSTDIR\${EXE_NAME}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}" "Publisher" "${PRODUCT_PUBLISHER}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}" "DisplayVersion" "${PRODUCT_VERSION}"
FunctionEnd
卸载-删除快捷方式
;卸载时删除任务栏快捷方式
Function un.DelBarlnk
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
${if} $R0 >= 6.0
;win10系统会有问题
ExecShell taskbarunpin "$DESKTOP\${PRODUCT_NAME}.lnk"
${StdUtils.InvokeShellVerb} $0 "$INSTDIR" "${EXE_NAME}" ${StdUtils.Const.ShellVerb.UnpinFromTaskbar}
Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
${else}
delete "$QUICKLAUNCH\${PRODUCT_NAME}.lnk"
${Endif}
FunctionEnd
;删除开始菜单,桌面图标
Function un.DeleteShotcutAndInstallInfo
SetRegView 32
DeleteRegKey HKLM "Software\${PRODUCT_PATHNAME}"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_PATHNAME}"
; 删除快捷方式
SetShellVarContext all
Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME}\卸载${PRODUCT_NAME}.lnk"
RMDir "$SMPROGRAMS\${PRODUCT_NAME}\"
Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
#删除开机启动
Delete "$SMSTARTUP\${PRODUCT_NAME}.lnk"
SetShellVarContext current
FunctionEnd
卸载删除开机启动
;卸载时删除开机启动
Function un.DelBootStart
DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "${PRODUCT_NAME}"
FunctionEnd
打开链接
; 打开链接
!define OpenURL '!insertmacro "_OpenURL"'
; 打开链接
!macro _OpenURL URL
Push "${URL}"
Call openLinkNewWindow
!macroend
; 新窗口打开链接
Function openLinkNewWindow
Push $3
Push $2
Push $1
Push $0
ReadRegStr $0 HKCR "http\shell\open\command" ""
# Get browser path
DetailPrint $0
StrCpy $2 '"'
StrCpy $1 $0 1
StrCmp $1 $2 +2 # if path is not enclosed in " look for space as final char
StrCpy $2 ' '
StrCpy $3 1
loop:
StrCpy $1 $0 1 $3
DetailPrint $1
StrCmp $1 $2 found
StrCmp $1 "" found
IntOp $3 $3 + 1
Goto loop
found:
StrCpy $1 $0 $3
StrCmp $2 " " +2
StrCpy $1 '$1"'
Pop $0
Exec '$1 $0'
Pop $0
Pop $1