MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_kernel32_version

Function get_kernel32_version

crates/vm/src/stdlib/sys.rs:1093–1157  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1091
1092 #[cfg(windows)]
1093 fn get_kernel32_version() -> std::io::Result<(u32, u32, u32)> {
1094 use crate::common::windows::ToWideString;
1095 unsafe {
1096 // Create a wide string for "kernel32.dll"
1097 let module_name: Vec<u16> = std::ffi::OsStr::new("kernel32.dll").to_wide_with_nul();
1098 let h_kernel32 = GetModuleHandleW(module_name.as_ptr());
1099 if h_kernel32.is_null() {
1100 return Err(std::io::Error::last_os_error());
1101 }
1102
1103 // Prepare a buffer for the module file path
1104 let mut kernel32_path = [0u16; MAX_PATH as usize];
1105 let len = GetModuleFileNameW(
1106 h_kernel32,
1107 kernel32_path.as_mut_ptr(),
1108 kernel32_path.len() as u32,
1109 );
1110 if len == 0 {
1111 return Err(std::io::Error::last_os_error());
1112 }
1113
1114 // Get the size of the version information block
1115 let ver_block_size =
1116 GetFileVersionInfoSizeW(kernel32_path.as_ptr(), core::ptr::null_mut());
1117 if ver_block_size == 0 {
1118 return Err(std::io::Error::last_os_error());
1119 }
1120
1121 // Allocate a buffer to hold the version information
1122 let mut ver_block = vec![0u8; ver_block_size as usize];
1123 if GetFileVersionInfoW(
1124 kernel32_path.as_ptr(),
1125 0,
1126 ver_block_size,
1127 ver_block.as_mut_ptr() as *mut _,
1128 ) == 0
1129 {
1130 return Err(std::io::Error::last_os_error());
1131 }
1132
1133 // Prepare an empty sub-block string (L"") as required by VerQueryValueW
1134 let sub_block: Vec<u16> = std::ffi::OsStr::new("").to_wide_with_nul();
1135
1136 let mut ffi_ptr: *mut VS_FIXEDFILEINFO = core::ptr::null_mut();
1137 let mut ffi_len: u32 = 0;
1138 if VerQueryValueW(
1139 ver_block.as_ptr() as *const _,
1140 sub_block.as_ptr(),
1141 &mut ffi_ptr as *mut *mut VS_FIXEDFILEINFO as *mut *mut _,
1142 &mut ffi_len as *mut u32,
1143 ) == 0
1144 || ffi_ptr.is_null()
1145 {
1146 return Err(std::io::Error::last_os_error());
1147 }
1148
1149 // Extract the version numbers from the VS_FIXEDFILEINFO structure.
1150 let ffi = *ffi_ptr;

Callers 1

getwindowsversionFunction · 0.85

Calls 6

newFunction · 0.85
to_wide_with_nulMethod · 0.80
as_mut_ptrMethod · 0.80
ErrClass · 0.50
as_ptrMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected