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


#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; }

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

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

 

posted on 2015-09-20 17:18  骷髅老人  阅读(805)  评论(0编辑  收藏  举报