sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
#[cfg(any(feature = "user", feature = "system"))]
pub(crate) unsafe fn to_utf8_str(p: windows::core::PWSTR) -> String {
    if p.is_null() {
        return String::new();
    }

    unsafe {
        p.to_string().unwrap_or_else(|_error| {
            sinf_debug!("Failed to convert to UTF-16 string: {}", _error);
            String::new()
        })
    }
}

cfg_select! {
    any(feature = "disk", feature = "system") => {
        use std::ops::Deref;
        use windows::Win32::Foundation::{CloseHandle, HANDLE};

        pub(crate) struct HandleWrapper(pub(crate) HANDLE);

        impl HandleWrapper {
            #[cfg(feature = "system")]
            pub(crate) fn new(handle: HANDLE) -> Option<Self> {
                if handle.is_invalid() {
                    None
                } else {
                    Some(Self(handle))
                }
            }

            #[cfg(feature = "disk")]
            pub(crate) unsafe fn new_from_file(
                drive_name: &[u16],
                open_rights: windows::Win32::Storage::FileSystem::FILE_ACCESS_RIGHTS,
                share_mode: windows::Win32::Storage::FileSystem::FILE_SHARE_MODE,
                flags_and_attributes: windows::Win32::Storage::FileSystem::FILE_FLAGS_AND_ATTRIBUTES,
            ) -> Option<Self> {
                let lpfilename = windows::core::PCWSTR::from_raw(drive_name.as_ptr());
                let handle = unsafe { windows::Win32::Storage::FileSystem::CreateFileW(
                    lpfilename,
                    open_rights.0,
                    share_mode,
                    None,
                    windows::Win32::Storage::FileSystem::OPEN_EXISTING,
                    flags_and_attributes,
                    Some(HANDLE::default()),
                ) }
                .ok()?;
                if handle.is_invalid() {
                    sinf_debug!(
                        "Expected handle to {:?} to be valid",
                        String::from_utf16_lossy(drive_name)
                    );
                    None
                } else {
                    Some(Self(handle))
                }
            }
        }

        impl Deref for HandleWrapper {
            type Target = HANDLE;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl Drop for HandleWrapper {
            fn drop(&mut self) {
                let _err = unsafe { CloseHandle(self.0) };
            }
        }
    }
    _ => {}
}