| |

VerySource

 Forgot password?
 Register
Search
View: 987|Reply: 0

NSIS判断是否以管理员权限运行源码

[Copy link]

4

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2023-9-26 13:57:11
| Show all posts |Read mode
本篇文章属于《518抽奖软件开发日志》系列文章的一部分。
我在开发《518抽奖软件》(www.518cj.net)的时候,NSIS安装包脚本里,需要判断当前是否管理员权限运行,代码如下。

  1. Function IsUserAdmin
  2. Push $R0
  3. Push $R1
  4. Push $R2

  5. ClearErrors
  6. UserInfo::GetName
  7. IfErrors Win9x
  8. Pop $R1
  9. UserInfo::GetAccountType
  10. Pop $R2

  11. StrCmp $R2 "Admin" 0 Continue
  12. ; Observation: I get here when running Win98SE. (Lilla)
  13. ; The functions UserInfo.dll looks for are there on Win98 too,
  14. ; but just don't work. So UserInfo.dll, knowing that admin isn't required
  15. ; on Win98, returns admin anyway. (per kichik)
  16. ; MessageBox MB_OK 'User "$R1" is in the Administrators group'
  17. StrCpy $R0 "true"
  18. Goto Done

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

  26. Win9x:
  27. ; comment/message below is by UserInfo.nsi author:
  28. ; This one means you don't need to care about admin or
  29. ; not admin because Windows 9x doesn't either
  30. ;MessageBox MB_OK "Error! This DLL can't run under Windows 9x!"
  31. StrCpy $R0 "true"

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

  34. Pop $R2
  35. Pop $R1
  36. Exch $R0
  37. FunctionEnd
Copy the Code


Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list