使能权限的代码

 1 BOOL SetPrivilege( HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege )
 2 {
 3     TOKEN_PRIVILEGES tkp;
 4 
 5     if ( LookupPrivilegeValue( NULL, lpszPrivilege, &tkp.Privileges[0].Luid ) )
 6     {
 7         m_tkp.PrivilegeCount = 1;
 8         m_tkp.Privileges[0].Attributes = ( bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0 );
 9         if ( AdjustTokenPrivileges( hToken, FALSE, &tkp, 0, NULL, NULL ) && GetLastError() == ERROR_SUCCESS )
10         {
11             return TRUE;
12         }
13     }
14 
15     return FALSE;
16 }
17 
18 int main()
19 {
20     HANDLE hToken;
21 
22     if ( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
23     {
24         SetPrivilege( hToken, SE_SHUTDOWN_NAME, TRUE );
25         SetPrivilege( hToken, SE_SHUTDOWN_NAME, FALSE );
26         CloseHandle( hToken );
27     }
28 
29     return 0;
30 }

 

posted on 2014-12-20 20:44  郁郁思扬  阅读(213)  评论(0编辑  收藏  举报

导航