微软发布了 Windows API 的官方 Rust 库

该库被设计为 MIT 许可证下的 Rust 箱,可以像这样使用:

[依赖项] windows =“0.2.1”

[构建依赖项] windows = "0.2.1"

之后,在 build.rs 构建脚本中,您可以生成应用程序所需的模块:

fn 主(){
窗口::构建!(
窗口::数据::xml::dom::*
windows::win32::system_services::{CreateEventW、SetEvent、WaitForSingleObject}
windows::win32::windows_programming::CloseHandle
);
}

有关可用模块的文档发布于 文档.rs.

示例 кода:

模组绑定 {
::windows::include_bindings!();
}

使用绑定::{
窗口::数据::xml::dom::*,
windows::win32::system_services::{CreateEventW,SetEvent,WaitForSingleObject},
windows::win32::windows_programming::CloseHandle,
};

fn main() -> windows::Result<()> {
让 doc = XmlDocument::new()?;
文档.load_xml(" 你好世界”)?;

让 root = doc.document_element()?;
断言!(root.node_name()?==“html”);
断言!(root.inner_text()?==“你好世界”);

不安全{
让事件 = CreateEventW(
std::ptr::null_mut(),
true.into(),
false.into(),
std::ptr::null(),
);

SetEvent(事件).ok()?;
WaitForSingleObject(事件, 0);
CloseHandle(事件).ok()?;
}

好的(())
}

一些函数调用使用不安全,因为这些函数是按原样提供的,没有使它们适应 Rust 约定。 Crate 的设计原理相同。 libc中,它用作访问 libc 的基本包,并用作构建具有安全接口的库的基础。


该项目创建于 Win32 元数据项目,旨在更轻松地为不同的编程语言创建 API。 第二个库,是在项目第一阶段基于元数据项目创建的—— C#/Win32。 微软还宣布开始工作 C++ 版本,使用现代风格的语言。

来源: linux.org.ru