Microsoft releases official Rust library for Windows API

The library is formatted as Rust crate under the MIT License, which can be used like this:

[dependencies]windows="0.2.1"

[build-dependencies]windows="0.2.1"

After that, in the build.rs build script, you can generate the modules that are needed for your application:

fnmain() {
windows::build!(
windows::data::xml::dom::*
windows::win32::system_services::{CreateEventW, SetEvent, WaitForSingleObject}
windows::win32::windows_programming::CloseHandle
);
}

Documentation about the available modules is published at docs.rs.

Example code:

mod bindings {
::windows::include_bindings!();
}

use bindings::{
windows::data::xml::dom::*,
windows::win32::system_services::{CreateEventW, SetEvent, WaitForSingleObject},
windows::win32::windows_programming::CloseHandle,
};

fn main() -> windows::Result<()> {
let doc = XmlDocument::new()?;
doc.load_xml(" hello world ")?;

let root = doc.document_element()?;
assert!(root.node_name()? == "html");
assert!(root.inner_text()? == "hello world");

unsafe {
let event = CreateEventW(
std::ptr::null_mut(),
true.into(),
false.into(),
std::ptr::null(),
);

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

Ok(())
}

For some function calls, unsafe is used, since these functions are provided as they are, without adapting them to Rust conventions. Crate is arranged according to the same principle libc, which serves as the base crate for accessing libc and is used as the basis for building libraries with a secure interface.


The project was created within the framework Win32 Metadata Project, which is designed to facilitate the creation of APIs for different programming languages. The second library that was created based on the Metadata Project in the first phase of the project is C#/Win32. Microsoft also announced the start of work on version for C++, which uses the modern style of the language.

Source: linux.org.ru