Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Last active December 5, 2023 10:49
Show Gist options
  • Save Aetopia/bfab230db2cf8d92b3798da52c2f5e59 to your computer and use it in GitHub Desktop.
Save Aetopia/bfab230db2cf8d92b3798da52c2f5e59 to your computer and use it in GitHub Desktop.
Get the most performant graphics adapter name as well as the currently installed CPU name using Rust.

Get the most performant graphics adapter name as well as the currently installed CPU name using Rust.

[dependencies.windows]
version = "0.52"
features = ["Win32_Foundation", "Win32_Graphics_Dxgi", "Win32_Graphics_Dxgi_Common"]
[dependencies.windows-sys]
version = "0.52"
features = ["Win32_Foundation", "Win32_Devices_DeviceAndDriverInstallation"]
use windows::Win32::Graphics::Dxgi::{
CreateDXGIFactory, IDXGIAdapter4, IDXGIFactory6, DXGI_ADAPTER_DESC3,
DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
};
use windows_sys::Win32::Devices::DeviceAndDriverInstallation::{
SetupDiEnumDeviceInfo, SetupDiGetClassDevsW, SetupDiGetDeviceRegistryPropertyW, DIGCF_PRESENT,
GUID_DEVCLASS_PROCESSOR, HDEVINFO, SPDRP_FRIENDLYNAME, SP_DEVINFO_DATA,
};
pub fn get_graphics_adapter_name() -> String {
let graphics_adapter_name: String;
unsafe {
let factory: IDXGIFactory6 = CreateDXGIFactory::<IDXGIFactory6>().unwrap();
let adapter: IDXGIAdapter4 = factory
.EnumAdapterByGpuPreference::<IDXGIAdapter4>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)
.unwrap();
let mut desc: DXGI_ADAPTER_DESC3 = std::mem::zeroed();
adapter
.GetDesc3(&mut desc as *mut DXGI_ADAPTER_DESC3)
.unwrap();
graphics_adapter_name = String::from_utf16_lossy(&desc.Description)
.trim_matches(char::from(0))
.to_string();
}
return graphics_adapter_name;
}
pub fn get_processor_name() -> String {
let processor_name: String;
unsafe {
let deviceinfoset: HDEVINFO =
SetupDiGetClassDevsW(&GUID_DEVCLASS_PROCESSOR, std::ptr::null(), 0, DIGCF_PRESENT);
let mut deviceinfodata: SP_DEVINFO_DATA = std::mem::zeroed();
deviceinfodata.cbSize = std::mem::size_of::<SP_DEVINFO_DATA>() as u32;
SetupDiEnumDeviceInfo(deviceinfoset, 0, &mut deviceinfodata);
let mut requiredsize: u32 = 0;
SetupDiGetDeviceRegistryPropertyW(
deviceinfoset,
&mut deviceinfodata,
SPDRP_FRIENDLYNAME,
std::ptr::null_mut(),
std::ptr::null_mut(),
0,
&mut requiredsize,
);
let mut propertybuffer: Vec<u8> = vec![0_u8; requiredsize as usize];
SetupDiGetDeviceRegistryPropertyW(
deviceinfoset,
&mut deviceinfodata,
SPDRP_FRIENDLYNAME,
std::ptr::null_mut(),
propertybuffer.as_mut_ptr(),
requiredsize,
std::ptr::null_mut(),
);
processor_name = String::from_utf8(propertybuffer).unwrap().replace("\0", "");
}
return processor_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment