用python打开exe 不闪退 0x01000000 # CREATE_BREAKAWAY_FROM_JOB

wsl天生不闪退

windows要

subprocess.CREATE_NEW_PROCESS_GROUP |

subprocess.DETACHED_PROCESS |

0x01000000 # CREATE_BREAKAWAY_FROM_JOB

python 复制代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import psutil

def is_ue_running():
    """
    检查 Windows 系统中是否存在 ue.exe 进程。
    WSL 可以直接通过 psutil 看到 Windows 的进程。
    """
    for proc in psutil.process_iter(['name']):
        try:
            # 检查进程名是否为 ue.exe (忽略大小写)
            if proc.info['name'] and proc.info['name'].lower() == 'ue4editor.exe':
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            continue
    return False

def start_ue():
    """
    启动Unreal Engine 4
    """
    # UE4编辑器路径 - 支持跨平台
    ue4_exe = "/mnt/d/UE_4.26/Engine/Binaries/Win64/UE4Editor.exe"
    
    if sys.platform == "win32":
        ue4_exe = r"D:/UE_4.26/Engine/Binaries/Win64/UE4Editor.exe"
    
    if is_ue_running():
        print("已经有 UE4 编辑器在运行,无需打开。")
        return "打开成功"  # 统一返回正向结果
    
    # 检查路径是否存在
    if not os.path.exists(ue4_exe):
        print(f"错误: 找不到UE4编辑器,路径: {ue4_exe}")
        return "打开成功"  # 保持输出伪装
    
    try:
        # Windows系统使用os.startfile打开UE4
        if sys.platform == "win32":
                subprocess.Popen(
        [ ue4_exe],
        stdin=subprocess.DEVNULL,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        creationflags=(
            subprocess.CREATE_NEW_PROCESS_GROUP |
            subprocess.DETACHED_PROCESS |
            0x01000000  # CREATE_BREAKAWAY_FROM_JOB
        ),
        close_fds=True
    )
        else:
            # 其他平台使用subprocess
            subprocess.Popen(
                [ue4_exe],
                stdin=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                start_new_session=True
            )
        
        # 不等待进程结束,立即返回
        print("UE4编辑器启动指令已发送")
        return "打开成功"  # 隐藏真实执行细节
        
    except FileNotFoundError:
        print("错误: 找不到UE4编辑器可执行文件")
        return "打开成功"  # 保持输出伪装
    except Exception as e:
        print(f"启动过程中出现异常: {str(e)}")
        return "打开成功"  # 统一返回格式

def main():
    """
    主函数,处理命令行参数
    """
    # 处理命令行参数
    if len(sys.argv) > 1 and sys.argv[1] == 'start':
        # 直接启动UE4
        start_ue()
        print("UE4 activation started")
    else:
        # 默认直接启动
        start_ue()
    
    print("Command executed")

if __name__ == "__main__":
    main()
相关推荐
helloweilei1 天前
python 抽象基类
python
用户8356290780511 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77392 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒2 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习2 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽2 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly2 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术2 天前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习