使用Python检查Windows是否存在PID而不需要库

时间:2013-07-12 17:38:41

标签: python windows

有没有办法在不需要库的情况下检查Windows上是否存在PID?怎么样?

2 个答案:

答案 0 :(得分:3)

用一小杯WINAPI解决了这个问题。

def pid_running(pid):
    import ctypes
    kernel32 = ctypes.windll.kernel32
    SYNCHRONIZE = 0x100000

    process = kernel32.OpenProcess(SYNCHRONIZE, 0, pid)
    if process != 0:
        kernel32.CloseHandle(process)
        return True
    else:
        return False

答案 1 :(得分:0)

这适用于我的系统..

>>> import subprocess
>>> out = subprocess.check_output(["tasklist","/fi","PID eq 1234"]).strip()
>>> if out == "INFO: No tasks are running which match the specified criteria.":
...   print "No such PID :D"
...
No such PID :D