Home > Back-end >  NSIS, judge whether the installation package an administrator to run the code
NSIS, judge whether the installation package an administrator to run the code

Time:05-14

This article belong to "518 supermarket broadcasting software development log" part of the series,
I in the development of "518 supermarket broadcast software" (http://www.518boyin.com/), made the installation package of NSIS package, install, need to make sure that is administrator privileges, if not, will prompt need administrator privileges to run, and then quit,

The Function UN. The onInit
Call the UN. IsUserAdmin
Pop $R0
${If} $R0=="false"
MessageBox MB_ICONEXCLAMATION | MB_OK | MB_TOPMOST | MB_SETFOREGROUND "should be run as an administrator $\ n"
The Quit
The ${EndIf}



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Usage:
; Call IsUserAdmin
; Pop $R0; At this point the $R0 is "true" or "false"
;
The Function IsUserAdmin
Push $R0
Push $R1
Push $R2

ClearErrors
The UserInfo: : GetName
IfErrors Win9x
Pop $R1
The UserInfo: : GetAccountType
Pop $R2

StrCmp $R2 "Admin" 0 Continue
; Observation: I get here the when running Win98SE. (Lilla)
; The functions provides The UserInfo. DLL looks for are there on Win98 with
; But just don 't work. So the UserInfo. DLL, knowing that the admin isn' t required
; On Win98, returns the admin anyway. (per kichik)
; MessageBox MB_OK 'User "$R1" is in the Administrators group'
StrCpy $R0 "true"
Goto the Done

The Continue:
; You should still check for an empty string because the functions provides
; The UserInfo. DLL looks for may not be present on Windows 95. (per kichik)
StrCmp $R2 "" Win9x
StrCpy $R0 "false"
; MessageBox MB_OK 'User "$R1 is" in the "$" R2 group'
Goto the Done

Win9x:
; The comment/message below is by the UserInfo. Nsi author:
; This one, which means you don 't need to care about the admin or
; Not the admin because Windows 9 x doesn 't either
; MessageBox MB_OK "Error! This DLL can 't run under Windows 9 x!"
StrCpy $R0 "true"

Done:
; MessageBox MB_OK 'User="$R1" AccountType="$R2" IsUserAdmin="$R0"'

Pop $R2
Pop $R1
Exch $R0
FunctionEnd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;