win32pdh.EnumObjectItems 调用错误
我在 Windows 7 中运行 Python 2.7,并安装了 pywin32-216-win32-py2.7
。我正在运行以下代码,它在一台计算机上运行良好,但在另一台计算机上输出错误(Win7、Py2.7、相同的 pywin 库)。
错误信息:
文件“C:\Energy\Support Sheets\Kill Excel.py”,第 9 行,在 GetProcessID 中 项目,实例= win32pdh.EnumObjectItems(无,无,对象,win32pdh.PERF_DETAIL_WIZARD) 错误:(-1073738824,“缓冲区大小的EnumObjectItems”,“在计算机上找不到指定的对象。”)
完整代码:
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!'
I'm running Python 2.7 in Windows 7, with pywin32-216-win32-py2.7
installed. I'm running the following code, and it runs fine on one computer but outputs an error on another (both Win7, Py2.7, same pywin library).
Error message:
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.')
Full code:
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!'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当进程性能计数器被禁用时,我遇到了同样的问题 - 显然这可能会发生。有说明此处了解如何重新启用计数器。如果您无法在计算机上安装软件,您可以运行 regedit 并搜索名为“禁用性能计数器”的键并将其删除。
之后,您可能需要以管理员身份在 cmd shell 上运行“lodctr /R”以重新加载性能计数器,然后才能正常工作。
I had the same issue when the Process performance counter was disabled - apparently this can just happen. There are instructions here for how to re-enable the counters. If you can't install software on the machine, you can run regedit and search for keys called "Disable Performance Counters" and delete them.
After that, you may need to run "lodctr /R" on a cmd shell as Administrator to reload the performance counters before it will work.
手动重建性能计数器解决了我的问题,您可以使用以下步骤手动重建性能计数器并与 Windows Management Instrumentation (WMI) 重新同步
重建计数器:
与 Windows Management Instrumentation (WMI) 重新同步计数器:
Manually rebuilding the performance counters solved my issue, you can use the following steps to manually rebuild and resync the performance counters with Windows Management Instrumentation (WMI)
Rebuild the counters:
Resync the counters with Windows Management Instrumentation (WMI):