VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 621|回复: 0

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

[复制链接]

4

主题

4

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2023-9-26 13:57:11 | 显示全部楼层 |阅读模式
本篇文章属于《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
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表