< 返回版块

ChenGuoHui 发表于 2023-05-26 21:36

Tags:windows-rs, w!

下面的代码,cargo build时,提示:

29 |         let url2 = windows::w!(url);
   |                                ^^^ no rules expected this token in macro call

use std::net::TcpListener;
use windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
use windows::Win32::System::Console::FreeConsole;
use std::net::TcpStream;
use std::io::{Read, Write};
use windows::Win32::UI::Shell::ShellExecuteW;

fn main() {
    // 监听端口7891的TCP连接
    let listener = TcpListener::bind("127.0.0.1:7891").unwrap();
    open_browser("http://127.0.0.1:7891");
    // 调用FreeConsole关闭控制台窗口
    unsafe {
       FreeConsole();
    }

    // 启动web server
    for stream in listener.incoming() {
        let stream = stream.unwrap();

        handle_client(stream);
    }


}
#[cfg(windows)]
fn open_browser(url: &str) {
        unsafe {
        let url2 = windows::w!(url);
        ShellExecuteW(None,
            None,
                       url2.as_ptr(),
                       None,
                       None,
                       SW_SHOWNORMAL);
    }
}



fn handle_client(mut stream: TcpStream) {
    let mut buffer = [0; 1024];
    stream.read(&mut buffer).unwrap();

    // 返回HTTP响应
    let response = "HTTP/1.1 200 OK\r\n\r\nHello, World!";

    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}

评论区

写评论
zhylmzr 2023-05-28 20:43
fn str_to_pcwstr(s: &str) -> Vec<u16> {
    let result = s
        .to_string()
        .encode_utf16()
        .chain(std::iter::once(0))
        .collect();
    result
}

PCWSTR::from_raw(str_to_pcwstr(name).as_ptr())
Bai-Jinlin 2023-05-27 10:20

这算什么bug呀,你用widestring库里的U16CString给str包一下,然后再用PCWSTR的from_raw,你可以参考下我这的这个


pub(crate) fn run_process(path: impl AsRef<Path>) {
    let exe_path = U16CString::from_os_str_truncate(path.as_ref().as_os_str());
    let exe_path = PCWSTR::from_raw(exe_path.as_ptr());
    let mut si = STARTUPINFOW::default();
    si.cb = mem::size_of::<STARTUPINFOW>() as _;
    let mut pi = PROCESS_INFORMATION::default();
    unsafe {
        let ret = CreateProcessW(
            exe_path,
            PWSTR::null(),
            None,
            None,
            false,
            PROCESS_CREATION_FLAGS::default(),
            None,
            PCWSTR::null(),
            &si,
            &mut pi,
        );
        if !ret.as_bool() {
            println!("{:?}", GetLastError());
        }
    }
}
vSylva 2023-05-27 00:21

w!宏只支持literal

    let url = "127.0.0.1";
    let mut url = url.to_string();
    url.push('\0');
    let url2 = url.encode_utf16().collect::<Vec<u16>>();
    let ws = PCWSTR::from_raw(url2.as_ptr());
    unsafe {
        println!("{}", ws.display());
    }
廴壬吉 2023-05-26 21:44

bug不bug不知道,但很难用

作者 ChenGuoHui 2023-05-26 21:38

现在还处于步步皆BUG阶段

[dependencies.windows]
version = "0.48"
features = [
    "Win32_System_Console",
    "Win32_Foundation",
    "Win32_UI_Shell",
    "Data_Xml_Dom",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
]

1 共 5 条评论, 1 页