winapi - 如何使用pywin32和WMI设置进程优先级?

  显示原文与译文双语对照的内容
0 0

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

我曾尝试过一个优先级类 ( 0 x40 ),以及一个实际的优先级( 4 ),但两者都失败了:


 File"test.py", line 5, in <module>
 proc.SetPriority(0x40)
 File"C:ProgsPython26libsite-packageswin32comclientdynamic.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)

时间: 原作者:

0 0

我遇到的问题与我玩'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]

原作者:
...