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

2024-04-19 19:09:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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),但都失败了,并显示以下消息:

^{pr2}$

我知道它不喜欢某个参数,但为什么不喜欢呢?在

我对使用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)

Tags: infromimportclientforprocwin32comse
1条回答
网友
1楼 · 发布于 2024-04-19 19:09:46

我在玩“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)

对于您的示例,可以跳过以下内容。要解析返回值,只需执行与输入相同的操作:

^{pr2}$

相关问题 更多 >