gpt4 book ai didi

python - win32pdh.EnumObjectItems 调用错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:04 26 4
gpt4 key购买 nike

我在 Windows 7 中运行 Python 2.7,安装了 pywin32-216-win32-py2.7。我正在运行以下代码,它在一台计算机上运行良好,但在另一台计算机上输出错误(Win7、Py2.7、相同的 pywin 库)。

错误信息:

File "C:\Energy\Support Sheets\Kill Excel.py", line 9, in GetProcessID items, instances = win32pdh.EnumObjectItems( None, None, object, win32pdh.PERF_DETAIL_WIZARD ) error: (-1073738824, 'EnumObjectItems for buffer size', 'The specified object was not found on the computer.')

完整代码:

import win32api, win32con
import win32pdh
import os
import signal
import time

def GetProcessID( name ) :
object = "Process"
items, instances = win32pdh.EnumObjectItems( None, None, object, win32pdh.PERF_DETAIL_WIZARD )
val = None
if name in instances :
hq = win32pdh.OpenQuery()
hcs = [ ]
item = "ID Process"
path = win32pdh.MakeCounterPath( ( None, object, name, None, 0, item ) )
hcs.append( win32pdh.AddCounter( hq, path ) )
win32pdh.CollectQueryData( hq )
time.sleep( 0.01 )
win32pdh.CollectQueryData( hq )

for hc in hcs:
type, val = win32pdh.GetFormattedCounterValue( hc, win32pdh.PDH_FMT_LONG )
win32pdh.RemoveCounter( hc )
win32pdh.CloseQuery( hq )
return val

def Kill_Process_pid(pid):
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
win32api.TerminateProcess(handle, -1) #kill by handle
win32api.CloseHandle(handle) #close api

def Kill_Process(name):
pid = GetProcessID(name)
if pid:
try:
Kill_Process_pid(pid)
return True
except:
pass
else:
return False

# MAIN FUNCTION
print 'Killing Excel instances...',
while Kill_Process('EXCEL'):
time.sleep(0.2)
print 'Done!'

最佳答案

当进程性能计数器被禁用时,我遇到了同样的问题 - 显然这可能会发生。有说明here了解如何重新启用计数器。如果您无法在机器上安装软件,您可以运行 regedit 并搜索名为“Disable Performance Counters”的键并将其删除。

之后,您可能需要以管理员身份在 cmd shell 上运行“lodctr/R”以重新加载性能计数器,然后才能正常工作。

关于python - win32pdh.EnumObjectItems 调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8931593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com