I am new to the Rust programming language, and I would like to share the notes on a problem I encountered and the solution I found.
At first, I attempted to use the [winapi](https://crates.io/crates/winapi) library in my Rust project. However, despite following the documentation, I encountered difficulties importing it. Additionally, it appears that the project is no longer actively maintained.
Hence, I have decided to utilize the crate provided by Microsoft for my needs.
## Windows crates
The crate that provide by Microsoft called **windows-rs**, you can find it on github.
### How to import
To incorporate the features provided by the crate into your project, you can refer to the example mentioned in the [README.md](https://github.com/microsoft/windows-rs#readme) and add the necessary configuration to your **Cargo.toml** file.
```=
[dependencies.windows]
version = "0.48"
features = [
"Data_Xml_Dom",
"Win32_Foundation",
"Win32_Security",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
]
```
In more detail, the term "Win32_UI_WindowsAndMessaging" refers to the user's intention of utilizing the functions within the `WindowsAndMessaging` component.
```rust=
use Win32::UI::WindowsAndMessaging::*;
```
If the user only adds the UI component, as shown below, they will be unable to utilize the `WindowsAndMessaging` functionality.
```=
[dependencies.windows]
version = "0.48"
features = [
"Data_Xml_Dom",
"Win32_Foundation",
"Win32_Security",
"Win32_System_Threading",
"Win32_UI",
]
```
## Problems
### How to convert `WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS` to `*const c_void`?
[solution](https://stackoverflow.com/questions/24191249/working-with-c-void-in-an-ffi)
In the project, I have to call `WlanHostedNetworkSetProperty` to set the wlan property. However, the structure of setting is `WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS` and I don't know how to cast it into `*const c_void`.
```rust=
let setting: WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS =
WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS {
hostedNetworkSSID: DOT11_SSID {
uSSIDLength,
ucSSID,
},
dwMaxNumberOfPeers: maximum_peek,
};
WlanHostedNetworkSetProperty(
self.handle,
wlan_hosted_network_opcode_connection_settings,
size_of::<WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS>() as u32,
setting,
Some(&mut reason),
None,
);
```
Then I found a post in stackoverflow. There is similiar question about casting to `*const c_void`. The solution shows that I can cast it like this.
```rust!
let setting_ptr: *const c_void = setting as *const _ as *const c_void;
```
### How to create `*mut *mut WLAN_HOSTED_NETWORK_STATUS` for `WlanHostedNetworkQueryStatus`?
[solution](https://stackoverflow.com/questions/42235980/create-mut-mut-to-a-struct)
```rust=
let mut status = Box::into_raw(Box::new(WLAN_HOSTED_NETWORK_STATUS::default()));
```