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

来源:互联网 发布:windows系统描述 编辑:程序博客网 时间:2024/06/05 19:23

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的安装包,原创