Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write windows native program with Rust

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to write windows native programs with Rust. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Nowadays, if the language world were to choose an online celebrity, it would undoubtedly be Rust. Rust has been accepted by various platforms because of its excellent security and efficiency. Linux kernel, Android underlying development and Windows underlying development have all adopted and given corresponding excuses. So is Microsoft's Windows, which recently released Rust for Windows v0.9. The update includes comprehensive call support, and Rust can call any Windows API directly and natively, which can greatly expand the development capability and scope of Rust under Windows.

Overview

This update provides many new features and updates, according to official information including:

Added support for Win32 and COM API, unified Windows crates. These Windows API have a new project win32metadata to add. For convenience and unification, the project name has been changed from "Rust/WinRT" to "Rust for Windows".

Several examples have been added to demonstrate how to call various Windows API, including Win32,COM and WinRT API.

Windows crates are released on crates.io and now support both MIT and Apache open source copyrights.

Built-in generated binding, no need to write manually.

Windows supports building on Linux.

Many improvements and fixes to Win32 API, such as support for array types, various string types, and updated metadata.

Added more natural and idiomatic support for the COM interface, such as return values, and support for other API involving C-style unions and nested types.

Reduced build time and improved error handling.

Retain the original API case, which affects existing code that uses Windows crate. Through the conversion of functions similar to QueryInterface to general-purpose functions, it is safer and easier to call many COM-related functions.

Environment configuration

The use of Window crates requires the first to configure the Rust development environment under Windows, and the installation in the Rust environment is also very simple and stupid.

Install rustup

First download the installation package rustup-init.exe from the Rust official (rust-lang.org) (remember to download the 32-bit or 64-bit version of the current Windows).

Then execute the installation package directly, and the installer automatically configures the system path, which can be used directly on the command line later, such as the cargo package manager.

Install C++ build tools

Rust compilation under Windows also depends on the Microsoft C++ build tools tool. If it is not installed, it will make an error when compiling that "link.exe" can not be found.

You need to download vs_buildtools from Microsoft VS. Select the C++ tool and Windows SDK components and install them.

Install VS Code and its Rust plug-in

In addition, it is generally recommended to install VS Code and its Rust plug-in for convenience:

Crates-io domestic sources

Configure the domestic source of Rust crates, because the official crates-io domestic download is too slow, or even easy to fail, so configure the domestic source first (such as ustc)

Create a config file in the user directory C:\ Users\ CC\ .clients, configured as follows:

Example

First, create a new Rust project through cargo:

Cargo new hello-chongchong

The above command creates a new directory and hello-chongchong creates the basic project framework directories and files.

Go to this directory and create dependent library items nested with the-- lib command:

Cargo new-lib bindings

Then pass through

Code.

Open the project in VS Code and take the screenshot as follows:

Modify the project Cargo.toml file to add the following dependency, which tells Cargo that it now depends on the newly created win library.

[dependencies] bindings = {path = "bindings"}

Now, in the Cargo.toml file under the win folder, add a dependency on the Windows crate, with the latest version specified as 0.9.1. This allows Windows support to be downloaded, built, and cached by allowing Cargo to package.

[dependencies] windows = "0.9.1" [build-dependencies] windows = "0.9.1"

Then create a new source file build.rs in the bindings directory and enter the source code:

/ / build.rs fn main () {windows::build! (Windows::Win32::WindowsAndMessaging::MessageBoxA);}

In the code, using the windows::build macro to specify the type to use, you can list again the API,Windows crates you need to use will directly generate the necessary bindings.

Then modify the following code in the win/src directory:

Windows::include_bindings! ()

In this way, the specified Windows API can be called arbitrarily in the main project main.rs file. Here we create a "Hello Chongchong!" Message dialog box.

Use bindings::Windows::Win32::WindowsAndMessaging:: {MessageBoxA, MESSAGEBOX_STYLE}; fn main () {unsafe {MessageBoxA (None, "Hello Chongchong!", "Message", MESSAGEBOX_STYLE::MB_OK);}}

Note that any Win32 function and COM interface method needs to be called in unsafe mode.

The project is then compiled through cargo build, and a dialog box pops up via cargo run.

The new version of the crate comes with a few more examples, which can be found in the examples directory of the windows-rs project repository.

Rust for Windows brings good news to rust in Windows development applications, although some API may have long been widely used, but now with official support, there can be great improvements in documentation, examples, stability, and so on.

The above is the editor for you to share how to use Rust to write windows native programs, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report