Home > Net >  Determine if a Windows executable is 32-bit or 64-bit
Determine if a Windows executable is 32-bit or 64-bit

Time:09-17

I'm trying to find out if an windows exe is 32-bit or 64-bit and I need to be able to do this on linux

Below I have very simplified code to visualize it

use std::{fs::File, path::Path};

fn is_x86_64(exe: &mut File) -> bool {
    // something
}

if let Ok(exe) = File::open(Path::new("./test.exe")) {
    assert!(is_x86_64(exe_file));
}

CodePudding user response:

The goblin crate looks like it provides what you need, and it seems to be pretty mature and used in production.

I was able to successfully run this on Linux:

use std::{error::Error, fs, path::Path};

fn is_x86_64(exe_data: &[u8]) -> Result<bool, Box<dyn Error>> {
    use goblin::Object;

    match Object::parse(exe_data)? {
        Object::PE(pe) => Ok(pe.is_64),
        _ => Err("File is not a Windows PE file.".into()),
    }
}

fn main() {
    let exe_data = fs::read(Path::new("./test.exe")).unwrap();
    println!("{:?}", is_x86_64(&exe_data).unwrap());
}
  • Related