Check if PID exists on Windows with Python without requiring libraries

python, windows

Solution

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

Problem

Is there a way to check if a PID exists on Windows with Python without requiring libraries? How to?

Original source

Related problems