您好,我想chcp 65001通過 windows api 呼叫系統從我的程式運行命令。
我的程式是用 rust 撰寫的,但使用外部 c。
extern "C" {
pub fn system(command: *const u8);
}
fn main() {
unsafe {
system("\"\"chcp\" \"65001\"\"".as_ptr());
}
// do work
}
該呼叫有效,但我無法格式化字串以system正確傳遞。
我在這個問題之后嘗試了以下組合。
"\"\"chcp\" \"65001\"\""(銹格式)
有了這個結果:Parameterformat wrong - "65001"
我還嘗試了其他一些變體但沒有成功:
"\"\"chcp\" 65001\""
結果:Parameterformat wrong - /rustc
"\"\"chcp 65001\""
結果 the system can't find the given path
第一個變體接縫很好,唯一的問題是 chcp 確實抱怨 \"
你有一個想法如何通過它嗎?
獎勵:為什么 '/rustc' 出現在變體 2 中。
謝謝您的幫助!
uj5u.com熱心網友回復:
Rust 字串不是以 NUL 結尾的,因此您對系統的呼叫正在讀取字串之后記憶體中的任何內容。我想這就是你的/rustc來源。您需要將您的字串轉換為 aCString以確保它被正確終止:
system (CString::new ("chcp 65001").unwrap().as_ptr());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409877.html
標籤:
下一篇:如何更改Windows顏色主題?
