4

有没有办法在不需要库的情况下使用 Python 检查 Windows 上是否存在 PID?如何?

4

2 回答 2

4

This is solved with a little cup of 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
于 2013-07-15T00:11:58.353 回答
1

这适用于我的系统..

>>> 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
于 2013-07-12T18:04:00.767 回答