欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

windows 用 python 来判断 是否 有 名为 epplcm.exe 进程 AG chatgpt 有大用 有大大用

在 Windows 上使用 Python 检查是否存在名为 epplcm.exe 的进程,可以通过 psutil 库(推荐)或标准的 ctypes 方法实现。以下是两种方法的详细代码示例:


方法 1:使用 psutil 模块(推荐)

python
import psutil def is_process_running(process_name):    for proc in psutil.process_iter(['name']):        if proc.info['name'] == process_name:            return True    return False if is_process_running("epplcm.exe"):    print("进程 epplcm.exe 正在运行。") else:    print("进程 epplcm.exe 未运行。")

安装 psutil(如果未安装):

bash
pip install psutil

方法 2:使用 ctypes(无需额外安装)

python
import ctypes from ctypes import wintypes def is_process_running(process_name):    # 定义 Windows API 函数    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)    CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot    Process32First = kernel32.Process32First    Process32Next = kernel32.Process32Next    CloseHandle = kernel32.CloseHandle    # 定义结构体    class PROCESSENTRY32(ctypes.Structure):        _fields_ = [            ('dwSize', wintypes.DWORD),            ('cntUsage', wintypes.DWORD),            ('th32ProcessID', wintypes.DWORD),            ('th32DefaultHeapID', ctypes.POINTER(wintypes.ULONG)),            ('th32ModuleID', wintypes.DWORD),            ('cntThreads', wintypes.DWORD),            ('th32ParentProcessID', wintypes.DWORD),            ('pcPriClassBase', wintypes.LONG),            ('dwFlags', wintypes.DWORD),            ('szExeFile', ctypes.c_char * 260)        ]    # 创建进程快照    hSnapshot = CreateToolhelp32Snapshot(2, 0)  # 0x00000002 = TH32CS_SNAPPROCESS    if hSnapshot == -1:        return False    entry = PROCESSENTRY32()    entry.dwSize = ctypes.sizeof(PROCESSENTRY32)    try:        # 遍历进程列表        if Process32First(hSnapshot, ctypes.byref(entry)):            while True:                if process_name.lower() == entry.szExeFile.decode().lower():                    return True                if not Process32Next(hSnapshot, ctypes.byref(entry)):                    break        return False    finally:        CloseHandle(hSnapshot) if is_process_running("epplcm.exe"):    print("进程 epplcm.exe 正在运行。") else:    print("进程 epplcm.exe 未运行。")

说明:

  1. 推荐使用 psutil:代码简洁且跨平台,适合大多数场景。

  2. ctypes 方法:无需安装第三方库,但代码较复杂,兼容性稍差。

  3. 进程名匹配:严格匹配文件名(如 epplcm.exe),大小写不敏感(通过 .lower() 处理)。

根据需求选择合适的方法即可。


普通分类: