• Rust使用Rust For Windows调用CreateProcessA


    .toml

    [dependencies.windows] 
    version = "0.51" 
    features = [
        "Win32_Foundation",
        "Win32_Security",
        "Win32_System_Threading",
        "Win32_System_Diagnostics_Debug",
        "Win32_System_Kernel",
        "Win32_System_Memory"
    ]
    
    [dependencies.encoding_rs]
    version = "0.8.33"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    main.rs

    use encoding_rs::GB18030;
    use std::ffi::CString;
    use windows::core::{PCSTR, PSTR};
    use windows::Win32::System::Threading::{
        CreateProcessA, CREATE_NEW_CONSOLE, PROCESS_INFORMATION, STARTUPINFOA,
    };
    
    fn my_create_prcoess(game_path: &str) {
        //初始化结构体
        let mut _process_info = PROCESS_INFORMATION::default();
        let mut _startup_info = STARTUPINFOA::default();
        _startup_info.cb = std::mem::size_of::<STARTUPINFOA>() as u32;
    
        //拼接命令行
        let game_path_f = format!("{}\\{}", game_path, "text.exe");
        println!("gamepath={}\n game_path_f={}", game_path, game_path_f);
    
        let commandline = convert_to_pcstr(&game_path_f).expect("Failed to convert string to GBK");
        let currentdirectory = convert_to_pcstr(game_path).expect("Failed to convert string to GBK");
        let success = unsafe {
            CreateProcessA(
                PCSTR::null(),
                PSTR(commandline.as_ptr() as *mut u8),
                None,
                None,
                false,
                CREATE_NEW_CONSOLE,
                None,
                PCSTR(currentdirectory.as_ptr() as *const u8),
                &mut _startup_info,
                &mut _process_info,
            )
        }
        .expect("CreateProcessA failed");
        println!("CreateProcessA 返回值: {:?} {:?}", success, _process_info);
    
    
    
    }
    
    //Utf8转GBK
    fn convert_to_pcstr(input: &str) -> Result<CString, std::io::Error> {
        let (converted, _, had_errors) = GB18030.encode(input);
        if had_errors {
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Conversion to ANSI failed",
            ))
        } else {
            Ok(CString::new(converted).unwrap())
        }
    }
    
    fn main() {
        my_create_prcoess("e:\\资源\\");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    UML——类图详解
    访问限制符说明面向对象的封装性
    分享 6 个 Vue3 开发必备的 VSCode 插件
    米尔电子|第十六届STM32全国研讨会即将走进11个城市
    redis-安装配置
    简单学懂链式编程
    【多线程与高并发】- synchronized锁的认知
    纪念DedeCMS创始人IT柏拉图先生
    计算机组成原理--数据表示
    域渗透-横向移动命令总结
  • 原文地址:https://blog.csdn.net/lunatic7/article/details/134204852