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
我已尝试传递一个优先级 (0x40) 以及一个实际优先级 (4),但都失败并显示以下消息:
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)
我可以看出它不喜欢某些参数,但为什么不呢?
我对使用 SetPriorityClass 的非 WMI 解决方案不感兴趣。我给了自己 SeDebugPrivilege 如下:
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)
win32security.AdjustTokenPrivileges(hToken, False, privs)
win32api.CloseHandle(hToken)
请您参考如下方法:
我在玩“GetOwner”时遇到了同样的问题。
刚试过这个,灵感来自 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)
对于您的示例,可以跳过以下内容。要解析返回值,只需执行与输入相同的操作:
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]
