Original link Address: http://blog.csdn.net/xbgprogrammer/article/details/7276760
We have a lot of operations that need to use the OpenProcess function, and in order for the program to have permission to use this function, we often take advantage of adjusttokenprivileges to elevate the privileges (not to mention the elevation, but to enable the disabled permissions in the access token)
BOOL Setprivilege (HANDLE htoken,//access token handleLPCTSTR Lpszprivilege,//name of privilege to Enable/disableBOOL Benableprivilege//To enable or disable privilege) {token_privileges tp; LUID LUID; if( !Lookupprivilegevalue (NULL,//Lookup privilege on Local systemLpszprivilege,//privilege to Lookup&LUID))//receives LUID of privilege{printf ("lookupprivilegevalue Error:%u\n", GetLastError ()); returnFALSE; } TP. Privilegecount=1; Tp. privileges[0]. Luid =Luid; if(benableprivilege) TP. privileges[0]. Attributes =se_privilege_enabled; ElseTP. privileges[0]. Attributes =0; //Enable the privilege or disable all privileges. if( !adjusttokenprivileges (Htoken, FALSE,&TP,sizeof(Token_privileges), (ptoken_privileges) null, (Pdword) null)) {printf ("adjusttokenprivileges Error:%u\n", GetLastError ()); returnFALSE; } if(GetLastError () = =error_not_all_assigned) {printf ("The token does not has the specified privilege. \ n"); returnFALSE; } returnTRUE;}voidMain () {HANDLE htoken; BOOL BRet= OpenProcessToken (getcurrentprocess (),token_all_access,&htoken); Setprivilege (htoken,se_debug_name,true);}
This code is no problem on XP, but if the program starts with a standard user on Windows 7 or Vista, AdjustTokenPrivileges will fail to start with the admin province.
This is because on Windows 7, there are few standard user permissions, no debug permissions, and, more than the ability to enable debug permissions, users can enable the console in both administrator and standard user mode, enter the command whoami/all
To see the different permissions under both permissions
PS: Even if you elevate debug permissions, it does not mean that calling OpenProcess to other processes will succeed (for example, system and AUDIODG processes under Win7 systems)
About the token_privileges structure
struct _token_privileges { DWORD privilegecount; *ptoken_privileges;
AdjustTokenPrivileges Enable permissions