winapi-rs icon indicating copy to clipboard operation
winapi-rs copied to clipboard

Issues with Importing Modules from winapi Crate in Rust

Open thiagolmoraes opened this issue 1 year ago • 3 comments

'm currently working on a Rust project where I need to use the winapi crate to allocate memory. However, I'm encountering issues with importing certain modules and functions from winapi. Here is a snippet of my Cargo.toml and main.rs:

[package]
name = "loader"
version = "0.1.0"
edition = "2021"

[dependencies]
sysinfo = "0.30.13"
winapi = { version = "0.3.9", features = [
    "winuser",
    "winbase",
    "synchapi",
    "processthreadsapi",
    "memoryapi",
    "handleapi",
    "tlhelp32",
    "winnt",
    "minwinbase"
] }

use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_EXECUTE_READWRITE};
use winapi::um::memoryapi::{VirtualAlloc, VirtualProtect};
use winapi::um::processthreadsapi::CreateThread;
use winapi::um::minwinbase::LPTHREAD_START_ROUTINE;
use winapi::ctypes::c_void;

...

thiagolmoraes avatar Dec 05 '24 14:12 thiagolmoraes

I found the solution to this issue through this helpful Stack Overflow answer. Here's what worked for me:

First, I updated my Cargo.toml file with the necessary features to use winapi:

[target.'cfg(windows)'.dependencies]
winapi = { 
    version = "0.3.9", 
    features = [
        "winnt", 
        "winuser", 
        "winbase", 
        "synchapi", 
        "processthreadsapi", 
        "memoryapi", 
        "handleapi", 
        "tlhelp32", 
        "minwinbase"
    ] 
}

Since I’m using Linux as my development environment, I set up cross-compilation for Windows by following these steps:

Add the Windows target: rustup target add x86_64-pc-windows-gnu Install mingw for compile Windows on Linux : sudo apt-get install mingw-w64

and then for build.

cargo build --target x86_64-pc-windows-gnu

thiagolmoraes avatar Dec 05 '24 16:12 thiagolmoraes

Given the weird way you respond with a solution to your own issue, I'm genuinely curious as to whether this is AI generated.

retep998 avatar Dec 05 '24 19:12 retep998

Given the weird way you respond with a solution to your own issue, I'm genuinely curious as to whether this is AI generated.

Sorry about that, actually I found the answer and asked him to form a solution based on that, I adjusted it

thiagolmoraes avatar Dec 06 '24 00:12 thiagolmoraes