[windows-rs]Rust 调用 Windows API

1、VSCode编辑器

下载安装VSCode
安装VSCode扩展

  1. rust-analyzerrust-analyzer(CN)(新手应该在rust-analyzer运行完毕后再动键盘)
  2. crates
  3. Better TOML

2、安装Microsoft C++ 生成工具

访问微软官网下载生成工具,勾选使用 C++ 的桌面开发之后会自动勾选5个项目
取消勾选以下项目
用于 Windows 的 C++ CMake 工具
测试工具核心功能 - 生成工具
C++ AddressSanitizer
只保留以下两个(SDK版本不用管,Win10或Win11都行,勾什么就留什么,不用自己勾)
MSVC v143 - VS 2022 C++ x64/x86 生成工具
Windows 11 SDK (10.0.22000.0)

3、安装rust

安装前必须重启电脑
访问Rust官网下载并安装Rust

4、设置rust镜像

运行where rustc找到.cargo文件夹
.cargo文件夹下新建无后缀文件,文件名为config,并输入以下内容,保存

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

5、创建项目

cargo new message-box
cd .\message-box\
cargo add windows
code .

6、获取windows-rs在电脑中的位置

  1. 查看文件.\message-box\Cargo.toml,记录当前依赖为windows = "0.48.0"且无特性features(后续依赖需要修改)
    [package]
    name = "message-box"
    version = "0.1.0"
    edition = "2021"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    windows = "0.48.0"
    
  2. 编辑文件.\message-box\src\main.rs
    在代码开头插入use语句
    use windows;
    
    这时出现存在warningwarning: unused import: `windows` ,不用管,本来也不用,一会就删了重写
  3. 按住Ctrl,左键单击windows跳转到文件
    请添加图片描述
    右键lib.rs文件,找到该文件在电脑中的位置
    请添加图片描述
  4. 删除添加的use语句
  5. 已获取lib.rs的路径:.\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\windows-0.48.0\src\lib.rs
    记录父文件夹:.\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\windows-0.48.0
    该文件夹为windows-rs在电脑中的位置

7、查找MessageBoxW特性

打开一个带有搜索文件内容功能的软件,例如ndd
目标目录:.\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\windows-0.48.0
查找目标:MessageBoxW
点击全部查找(把它肠子翻烂,比看demo简单多了,官方demo是大杂烩,根本不知道引用的哪里)
在这里插入图片描述

处理搜索结果

  1. readme.md:无法确认使用了什么特性,跳过。
  2. Web/InternetExplorer/impl.rs
    关键字:fn MessageBoxW
    未提到特性,跳过。
  3. Web/InternetExplorer/mod.rs
    关键字:pub unsafe fn MessageBoxW
    特性:#[doc = "*Required features: `\"Win32_Foundation\"`*"]
    这个分类应该对应IE - 微软文档
  4. UI/WindowsAndMessaging/mod.rs
    关键字:pub unsafe fn MessageBoxW
    特性:#[doc = "*Required features: `\"Win32_UI_WindowsAndMessaging\"`, `\"Win32_Foundation\"`*"]
    查看该文件,很多常量与winuser.h有关
    找到了MessageBoxW,特性为Win32_UI_WindowsAndMessagingWin32_Foundation
  5. UI/Shell/mod.rs
    关键字:pub unsafe fn ShellMessageBoxW
    名字都对不上,跳过。
  6. Grapics/Printing/mod.rs
    关键字:pub unsafe fn PrinterMessageBoxW
    名字都对不上,跳过。

MessageBoxW特性
找到了MessageBoxW,特性为Win32_UI_WindowsAndMessagingWin32_Foundation

8、修改Cargo.toml

修改文件.\message-box\Cargo.toml

[package]
name = "message-box"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies.windows]
version = "0.48.0"
features = [
    "Win32_UI_WindowsAndMessaging",
    "Win32_Foundation",
]

运行cargo run,输出了Hello, world!

9、编写代码

编辑文件.\message-box\src\main.rs
整段代码手打,全程代码提示,打的最多的就是双冒号。
参考文档:MessageBoxW 函数 (winuser.h) - 微软文档

#![windows_subsystem = "windows"]

use windows::w;
use windows::Win32::UI::WindowsAndMessaging::{
    
    MessageBoxW, MB_ICONINFORMATION, MB_OK};

fn main() {
    
    
    println!("Hello, world!");
    unsafe {
    
    
        MessageBoxW(None, w!("Hello"), w!("World"), MB_ICONINFORMATION | MB_OK);
    }
}

运行cargo run,显示对话框
在这里插入图片描述
至此,用Rust(exe160,256字节)实现了vbs(转exe31,232字节)一句话就完成的程序

MsgBox "Hello", vbInformation + vbOKOnly, "World"

10、发布

cargo build --release
exe文件:.\message-box\target\release\message-box.exe



请添加图片描述

猜你喜欢

转载自blog.csdn.net/qq_39124701/article/details/129969337