Esempio n. 1
0
 private static extern int AdjustTokenPrivileges(IntPtr TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
Esempio n. 2
0
 /// <summary>
 /// 启用指定的权限
 /// </summary>
 /// <param name="privilege">要启用的权限</param>
 /// <exception cref="PrivilegeException">表明在申请相应权限时发生了错误</exception>
 protected static void EnableToken(string privilege)
 {
     if (!CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges"))
         return;
     IntPtr tokenHandle = IntPtr.Zero;
     LUID privilegeLUID = new LUID();
     TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
     TOKEN_PRIVILEGES tokenPrivileges;
     if (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
         throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
     if (LookupPrivilegeValue("", privilege, ref privilegeLUID) == 0)
         throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
     tokenPrivileges.PrivilegeCount = 1;
     tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
     tokenPrivileges.Privileges.pLuid = privilegeLUID;
     int size = 4;
     if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
         throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
 }