如何使用 pywin32 和 WMI 设置进程优先级? [英] How to set process priority using pywin32 and WMI?

查看:29
本文介绍了如何使用 pywin32 和 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

我尝试传递一个优先级 (0x40) 以及一个实际的优先级 (4),但都失败并显示以下消息:

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?

我对使用 SetPriorityClass 的非 WMI 解决方案不感兴趣.我给自己的 SeDebugPrivilege 如下:

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)
win32security.AdjustTokenPrivileges(hToken, False, privs)
win32api.CloseHandle(hToken)

推荐答案

我在玩GetOwner"时遇到了同样的问题.

I encountered the same problem as I was playing with 'GetOwner'.

刚试过这个,灵感来自 WMI:

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]

这篇关于如何使用 pywin32 和 WMI 设置进程优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆