nsxz85
判断当前程序是否以管理员身份运行,代码如下:


#include <iostream> #include <windows.h> using namespace std; // 判断当前程序是否以管理员身份运行。 bool IsProcessRunAsAdmin() { SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; PSID AdministratorsGroup; BOOL ret= AllocateAndInitializeSid( &NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup); if (ret) { CheckTokenMembership(NULL, AdministratorsGroup, &ret); FreeSid(AdministratorsGroup); } return ret== TRUE ; } int main() { cout << "Hello world!" << endl; if(IsProcessRunAsAdmin()) { cout<<"Run as administrator ........"<<endl; } else { cout<<"Run as Not Administrator ......"<<endl; } getchar(); return 0; }

程序结果:
普通方式运行(直接双击运行):

以管理员方式运行(右键->以管理员身份运行)-->确定:

 

分类:

技术点:

相关文章:

  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-01-02
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2021-12-11
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
相关资源
相似解决方案