Microsoft has released an official Rust library for the Windows API

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

[dependencies]
windows = "0.2.1"

[build-dependencies]
windows = "0.2.1"

After this, you can generate the necessary modules for your application in the build script build.rs:

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

Documentation for available modules is published at docs.rs.

Code example:

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("<html>hello world</html>")?;

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 because these functions are provided as they are, without adapting them to Rust conventions. The crate is structured similarly, libcwhich serves as the base crate for accessing libc and is used as the foundation for building libraries with a safe interface.


The project was created within the Win32 Metadata Project,which aims to simplify the creation of APIs for various programming languages. The second library created based on the Metadata Project in the initial phase is C#/Win32.Microsoft also announced the start of work on a version for C++,which utilizes the modern style of the language.

Source: linux.org.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster