NSIS脚本,检查权限和.NET环境 - (摘录及原创)

本文介绍了如何使用NSIS脚本来检查安装程序的管理员权限,并检测.NET环境。通过提供的CheckAdmin和CheckDotNet函数,可以确保只有管理员才能运行安装程序,并在缺少.NET环境时提示用户进行安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSIS 官网:http://nsis.sourceforge.net/Main_Page

一款很好用的NSIS脚本编辑器(HM NIS EDIT: A Free NSIS Editor/IDE):http://hmne.sourceforge.net/

---------------------------------------

; Assist Functions, CheckAdmin, CheckPlugin, CheckQB, CheckDotNet, InstallDotNet, CheckPluginUsing
Function CheckAdmin
  ClearErrors
  UserInfo::GetName
  IfErrors Win9x
  Pop $0
  UserInfo::GetAccountType
  Pop $1
  # GetOriginalAccountType will check the tokens of the original user of the
  # current thread/process. If the user tokens were elevated or limited for
  # this process, GetOriginalAccountType will return the non-restricted
  # account type.
  # On Vista with UAC, for example, this is not the same value when running
  # with `RequestExecutionLevel user`. GetOriginalAccountType will return
  # "admin" while GetAccountType will return "user".

  UserInfo::GetOriginalAccountType
  Pop $2
  StrCmp $1 "Admin" 0 +3
          ;MessageBox MB_OK 'User "$0" is in the Administrators group${REALMSG}'
          Goto done
          StrCmp $1 "Power" 0 +3
   ;MessageBox MB_OK 'User "$0" is in the Power Users group${REALMSG}'
   Goto exit
  StrCmp $1 "User" 0 +3
          ;MessageBox MB_OK 'User "$0" is just a regular user${REALMSG}'
   Goto exit
   StrCmp $1 "Guest" 0 +3
          ;MessageBox MB_OK 'User "$0" is a guest${REALMSG}'
          Goto exit
          MessageBox MB_OK "Unknown error"
          Goto exit
  Win9x:
  # This one means you don't need to care about admin or
  # not admin because Windows 9x doesn't either

  MessageBox MB_OK "Error! This DLL can't run under Windows 9x!"
  exit:
  MessageBox MB_OK "Only Administrator can run this application. Please run it again as Administrator."
  Quit
  done:
FunctionEnd

;以上方法来自NSIS自带例子


Function CheckDotNet
 
Call GetDotNETVersion
  Pop $0
  StrCmp $0 "not found" update1 check ; not found any .net library
  check:
  StrCpy $0 $0 "" 1 ;skip "v"
  StrCpy $0 $0 3 ;get the version as "x.x"
  StrCpy $2 $0 1 ;get the major version number (first x from x.x)
  StrCmp $2 "1" update2 going
  update1:
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON1 ".NET runtime library is not installed. Installer will stop to help you to install .NET 2.0 runtime library. After .NET 2.0 installing, please run installer again to finish installation. Do you want to install .NET 2.0 runtime library?" IDYES install IDNO exit
  update2:
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON1 ".NET runtime library v2.0 or newer is required. You have v$0 installed. Installer will stop to help you to update it to .NET 2.0 runtime library. After .NET 2.0 installing, please run installer again to finish installation. Do you want to install .NET 2.0 runtime library?" IDYES install IDNO exit
  install:
  Call InstallDotNet
  goto going
  exit:
  Quit
  going:
FunctionEnd

;以上方法来自NSIS自带帮助


; Get the current .Net runtime liberary version
Function GetDotNETVersion
  Push $0
  Push $1
  System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1 ?u"
  StrCmp $1 "error" 0 +2
    StrCpy $0 "not found"
  Pop $1
  Exch $0
FunctionEnd

; 以上方法来自NSIS官方网站


; Install .NET 2.0
Function InstallDotNet
  Call UnZipDotNetInstaller
  ExecWait "$TEMP/dotnetfx.exe"
  Quit
FunctionEnd

;简单方法,用于调用.NET的安装包,原创


; Unzip .NET 2.0 Installer
Function UnZipDotNetInstaller
SetOutPath "$TEMP"
  SetOverwrite off
  File "Resource/dotnetfx.exe"
FunctionEnd

;简单方法,用于解包.NET的安装包,原创

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值