Taptiive / machineid-rs

Create a unique MachineID/HWID/UUID with customizable options, like .Net DeviceId
64 stars 26 forks source link

[tauri] Cannot get DriveSerial in Windows #5

Closed kiryasolod closed 1 year ago

kiryasolod commented 1 year ago

Hello, dear author! I cannot get a drive serial number. I guess the problem is in wmi library. I got the following message: thread 'main' panicked at 'called Result::unwrap() on an Err value: HResultError { hres: -2147417831 }', C:\Users\Garrus\.cargo\registry\src\github.com-1ecc6299db9ec823\machineid-rs-1.2.2\src\windows.rs:35:52

My tauri code is

#[tauri::command]
fn check_license() {
    println!("Machine ID: {}", license::get_pc_id().unwrap());
}

Where license::get_pc_id() is the following function:

pub fn get_pc_id() -> Result<String, impl std::error::Error> {
    let mut builder = IdBuilder::new(Encryption::SHA256);
    builder
        .add_component(HWIDComponent::CPUID)
        .add_component(HWIDComponent::DriveSerial)
        .add_component(HWIDComponent::SystemID)
        .add_component(HWIDComponent::CPUCores);
    builder.build("blablabla")
}

It works if I don't use DriveSerial but I really need it. Please help

kiryasolod commented 1 year ago

Solved this problem myself by invoking my code in main function before tauri system is initialized:

fn main() {
    let pc_id_result = license::get_pc_id();
    let builder = tauri::Builder::default();
    builder
        .xmldos_setup(pc_id_result)
        .on_menu_event(move |event| {
            handle_main_menu_action(event.menu_item_id());
        })
        .xmldos_invoke_handler()
        .run(tauri::generate_context!())
        .expect("error while running tauri application")
}

My fn main code is not standard: most functions are encapsulated in other source files but it's still clear enough for other developers to understand how to use this library with tauri. I think, most people will invoke the library just once, before the app is initialized. I tried to run it in other thread but it does not work. So this is my working solution