How to set process priority using pywin32 and WMI?
from win32com.client import GetObject
for proc in GetObject("WinMgmts:{impersonationLevel=impersonate,(IncreaseBasePriority,Debug)}").InstancesOf("Win32_Process"):
if proc.CommandLine == "<my-command-line>":
proc.SetPriority(4) # fails
I have tried passing a priority class (0x40) as well as an actual priority (4), but both fail with the following message:
File "test.py", line 5, in <module>
proc.SetPriority(0x40)
File "C:\Progs\Python26\lib\site-packages\win32com\client\dynamic.py", line 505, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'SWbemObjectEx', u'Invalid parameter ', None, 0, -2147217400), None)
I can see that it's not liking some parameter, but why not?
I'm not interested in a non-WMI solution using SetPriorityClass
. I have given myself the SeDebugPrivilege
as follows:
import win32security, ntsecuritycon, win32con, win32api
privs = ((win32security.LookupPrivilegeValue('',ntsecuritycon.SE_DEBUG_NAME), win32con.SE_PRIVILEGE_ENABLED),)
hToken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), win32security.TOKEN_ALL_ACCESS)
w开发者_StackOverflow社区in32security.AdjustTokenPrivileges(hToken, False, privs)
win32api.CloseHandle(hToken)
I encountered the same problem as I was playing with 'GetOwner'.
Just tried this, inspired from WMI:
# using proc as in your code
# this line seems to provide the dispatch interface on the COM object
disp = Dispatch(proc)
# this one gets the method definition
method = disp.Methods_('SetPriority')
# the input parameters, and their description
in_parameters = method.InParameters
in_parameter_names = [(i.Name, i.IsArray) for i in in_parameters.Properties_] \
if not in_parameters is None else [] # not needed here
# >> print in_parameter_names
# [(u'Priority', False)]
# the priority parameter, and setting its value
in_parameters.Properties_['Priority'].Value = 0x40
# doing the call
return_values = disp.ExecMethod_ (method.Name, in_parameters)
For your sample, the following could be skipped. To parse the return values, just do the same as the inputs:
out_parameters = method.OutParameters
out_parameter_names = [(i.Name, i.IsArray) for i in out_parameters.Properties_] \
if not out_parameters is None else []
res = [return_values.Properties_(i[0]).Value for i in out_parameter_names]
精彩评论