美文网首页
python子线程中WMI报错-2147221020解决

python子线程中WMI报错-2147221020解决

作者: 耀看 | 来源:发表于2018-12-07 21:07 被阅读0次

在编写检测进程的程序的时候发现如下错误:

def check_exsit(process_name):
    WMI = win32com.client.GetObject('winmgmts:')
    processCodeCov = WMI.ExecQuery('select * from Win32_Process where Name="%s"' % process_name)
    if len(processCodeCov) > 0:
        print ('%s is exists' % process_name)
    else:
        win32process.CreateProcess('C:\Program Files (x86)\Tektronix\VectorVuPC\VectorVu-PC.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO())
        print ('%s is not exists' % process_name)
失败报错

这个程序是在子线程中运行的,经过查询得知在子线程中运行WMI需要进行初始化,改为如下程序:

def check_exsit(process_name):
    pythoncom.CoInitialize()
    WMI = win32com.client.GetObject('winmgmts:')
    processCodeCov = WMI.ExecQuery('select * from Win32_Process where Name="%s"' % process_name)
    if len(processCodeCov) > 0:
        print ('%s is exists' % process_name)
    else:
        win32process.CreateProcess('C:\Program Files (x86)\Tektronix\VectorVuPC\VectorVu-PC.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO())
        print ('%s is not exists' % process_name)

相关文章

网友评论

      本文标题:python子线程中WMI报错-2147221020解决

      本文链接:https://www.haomeiwen.com/subject/efixhqtx.html