Search code examples
winapirustinteropunsafeabi

Rust WINAPI outputting HMODULE value


I am trying to write a simle rust console application that calls into WINAPI.

I will ommit the obvious use and extern crate parts of my code. Here it is:

fn win32_string(value : &str ) -> Vec<u16> {
    OsStr::new( value ).encode_wide().chain( once( 0 ) ).collect()
}

fn main() {
    println!("=====  Rust Windows experiment #1  =====");

    let module_name = win32_string("ntdll.dll");

    let h_instance: HMODULE;

    unsafe {
        h_instance = GetModuleHandleW(module_name.as_ptr());
    }

    println!("Value of h_instance: {:#?}", h_instance);
}

I am building it against the target triple:

[build]
target = "i686-pc-windows-msvc"

As you can see I am targeting to build a 32 bit application. Now the output of my program is the following:

=====  Rust Windows experiment #1  =====
Value of h_instance: 0x00007ffb61c40000

It is showing a 64 bit address. How can this happen? Am I writing the HMODULE value wrongly to the console, or what am I doing wrong?


Solution

  • With help from rodrigo: The build tag is supposed to be included in the .cargo\config file instead of the cargo.toml file, in order for the compiler to pick it up. :) After changing that the issue is now fixed. :)