Link Error during Rust compilation (Cargo)

1

I'm calling some WinAPI functions with the Rust FFI (in this case MessageBoxA).

My code was working until I did a little variable change and the compilation gave me an error as:

= note: Non-UTF-8 output: WinAPI-dd8845a539e186b8.4ojwfrbxjnkzuhga.rcgu.o : er
ror LNK2019: symbole externe non r\xe9solu MessageBoxA r\xe9f\xe9renc\xe9 dans l
a fonction _ZN6WinAPI4main17hdf93991da0bc3966E\r\nd:\\core\\Confidential\\Forens
ic\\Rust\\WinAPI\\target\\debug\\deps\\WinAPI-dd8845a539e186b8.exe : fatal error
 LNK1120: 1 externes non r\xe9solus\r\n

The last line is in french and it means LNK1120: 1 unresolved external.

I kind of understand that is an encoding error but I have no ideas how to solve it.

So I canceled the little changes I did in my code but it keep displaying that weird message (The error message is actually bigger but not comprehensible).

It's actually a cargo project, the code if you want to check it:

#[cfg(windows)]
#[link(name = "user32", kind = "dylib")]
extern crate libc;
mod ffi{
    use libc::{c_uint,uintptr_t,c_void};
    type HANDLE = *mut c_void;
    pub type UINT = c_uint;
    pub type UINT_PTR = uintptr_t;
    pub type HWND = HANDLE;
    pub type LPCTSTR = *const i8;
    pub const MB_OK: u32 = 0x0;
    pub const MB_OKCANCEL: u32 = 0x00000001;
    pub const MB_ICONWARNING: u32 = 0x00000030;
    pub const MB_ICONINFORMATION: u32 = 0x00000040;
    pub const MB_ICONQUESTION: u32 = 0x00000020;
}
extern "system"{
    fn MessageBoxA(hWnd: ffi::HWND, lpText: ffi::LPCTSTR, lpCaption: ffi::LPCTSTR, uType: u32) -> u32;
}
use ffi::LPCTSTR;
use ffi::MB_OK;
use ffi::MB_ICONINFORMATION;
fn main() -> std::io::Result<()>{
    unsafe{
        let buffer: &[u8] = &[97,99,107,101,0]; // "acke" as a null terminated str
        let lpData: LPCTSTR = core::str::from_utf8_unchecked(buffer).as_ptr() as *const i8;
        let lpCaption: LPCTSTR = "Information".as_ptr() as *const i8;
        MessageBoxA(
            std::ptr::null_mut(),
            lpData,
            lpCaption,
            MB_OK | MB_ICONINFORMATION,
        );
    };
    return Ok(());
}
#[cfg(not(windows))]
fn main() -> std::io::Result<()>{
    println!("That program only runs on Windows 10 architectures.");
    return Ok(());
}

Important : The error doesn't occure when I put the call to MessageBoxA in comment.

winapi
rust
linker
ffi
rust-cargo
asked on Stack Overflow Sep 13, 2019 by centipede_ • edited Sep 15, 2019 by centipede_

1 Answer

0

I changed my Visual Studio default language to English because of encoding problems with link.exe.

Then I got an error about unresolved external: MessageBoxA@16. To solve that, I moved the line extern "system"{ /* Functions prototypes */ } directly after #[link(name="user32")] declaration.

You also need Windows 10 SDK installed.

Thanks to rustup.exe for giving really good installation indications!

answered on Stack Overflow Sep 16, 2019 by centipede_ • edited Sep 16, 2019 by Shepmaster

User contributions licensed under CC BY-SA 3.0