(privilegeStr, hToken = None)
Enable Privilege on token, if no token is given the function gets the token of the current process.
Enable Privilege on token, if no token is given the function gets the token of the current process.
Enable Privilege on token if no token is given the function gets the token of the current process .
def EnablePrivilege(privilegeStr, hToken = None):
	"""Enable Privilege on token, if no token is given the function gets the token of the current process."""
	close=False
	if hToken == None:
		close=True
		TOKEN_ADJUST_PRIVILEGES = 0x00000020
		TOKEN_QUERY = 0x0008
		hToken = HANDLE(INVALID_HANDLE_VALUE)
		res=windll.advapi32.OpenProcessToken( windll.kernel32.GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), byref(hToken) )
  
	privilege_id = LUID()
	res=windll.advapi32.LookupPrivilegeValueA(None, privilegeStr, byref(privilege_id))

	SE_PRIVILEGE_ENABLED = 0x00000002
	laa = LUID_AND_ATTRIBUTES(privilege_id, SE_PRIVILEGE_ENABLED)
	tp = TOKEN_PRIVILEGES(1, laa)

	ERROR_NOT_ALL_ASSIGNED=1300

	res=windll.advapi32.AdjustTokenPrivileges(hToken, False, byref(tp), sizeof(tp), None, None)
	if not res:
		raise WinError()
	else:
		res=windll.kernel32.GetLastError()
		if res!=0:
			raise WinError()
	if close:
		windll.kernel32.CloseHandle(hToken)
def EnablePrivilege ( privilegeStr , hToken = None ) : close = False if hToken == None : close = True TOKEN_ADJUST_PRIVILEGES = 0x00000020 TOKEN_QUERY = 0x0008 hToken = HANDLE ( INVALID_HANDLE_VALUE ) res = windll . advapi32 . OpenProcessToken ( windll . kernel32 . GetCurrentProcess ( ) , ( TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY ) , byref ( hToken ) ) privilege_id = LUID ( ) res = windll . advapi32 . LookupPrivilegeValueA ( None , privilegeStr , byref ( privilege_id ) ) SE_PRIVILEGE_ENABLED = 0x00000002 laa = LUID_AND_ATTRIBUTES ( privilege_id , SE_PRIVILEGE_ENABLED ) tp = TOKEN_PRIVILEGES ( 1 , laa ) ERROR_NOT_ALL_ASSIGNED = 1300 res = windll . advapi32 . AdjustTokenPrivileges ( hToken , False , byref ( tp ) , sizeof ( tp ) , None , None ) if not res : raise WinError ( ) else : res = windll . kernel32 . GetLastError ( ) if res != 0 : raise WinError ( ) if close : windll . kernel32 . CloseHandle ( hToken )