|
2 | 2 |
|
3 | 3 | use std::path::PathBuf;
|
4 | 4 |
|
5 |
| -use crate::translate::*; |
| 5 | +use crate::{translate::*, GString, StrV}; |
6 | 6 |
|
7 |
| -#[doc(alias = "g_win32_get_package_installation_directory_of_module")] |
8 |
| -pub fn win32_get_package_installation_directory_of_module( |
9 |
| - hmodule: ffi::gpointer, |
10 |
| -) -> Option<PathBuf> { |
| 7 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] |
| 8 | +pub enum OSType { |
| 9 | + #[doc(alias = "G_WIN32_OS_ANY")] |
| 10 | + Any, |
| 11 | + #[doc(alias = "G_WIN32_OS_WORKSTATION")] |
| 12 | + Workstation, |
| 13 | + #[doc(alias = "G_WIN32_OS_SERVER")] |
| 14 | + Server, |
| 15 | +} |
| 16 | + |
| 17 | +#[doc(hidden)] |
| 18 | +impl IntoGlib for OSType { |
| 19 | + type GlibType = ffi::GWin32OSType; |
| 20 | + |
| 21 | + #[inline] |
| 22 | + fn into_glib(self) -> Self::GlibType { |
| 23 | + match self { |
| 24 | + Self::Any => ffi::G_WIN32_OS_ANY, |
| 25 | + Self::Workstation => ffi::G_WIN32_OS_WORKSTATION, |
| 26 | + Self::Server => ffi::G_WIN32_OS_SERVER, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[doc(alias = "g_win32_check_windows_version")] |
| 32 | +pub fn check_windows_version(major: i32, minor: i32, spver: i32, os_type: OSType) -> bool { |
11 | 33 | unsafe {
|
12 |
| - from_glib_full(ffi::g_win32_get_package_installation_directory_of_module( |
13 |
| - hmodule, |
| 34 | + from_glib(ffi::g_win32_check_windows_version( |
| 35 | + major, |
| 36 | + minor, |
| 37 | + spver, |
| 38 | + os_type.into_glib(), |
14 | 39 | ))
|
15 | 40 | }
|
16 | 41 | }
|
| 42 | + |
| 43 | +#[doc(alias = "g_win32_get_command_line")] |
| 44 | +#[doc(alias = "get_command_line")] |
| 45 | +pub fn command_line() -> StrV { |
| 46 | + unsafe { FromGlibPtrContainer::from_glib_full(ffi::g_win32_get_command_line()) } |
| 47 | +} |
| 48 | + |
| 49 | +#[doc(alias = "g_win32_error_message")] |
| 50 | +pub fn error_message(error: i32) -> GString { |
| 51 | + unsafe { from_glib_full(ffi::g_win32_error_message(error)) } |
| 52 | +} |
| 53 | + |
| 54 | +#[doc(alias = "g_win32_getlocale")] |
| 55 | +pub fn getlocale() -> GString { |
| 56 | + unsafe { from_glib_full(ffi::g_win32_getlocale()) } |
| 57 | +} |
| 58 | + |
| 59 | +#[doc(alias = "g_win32_get_package_installation_directory_of_module")] |
| 60 | +#[doc(alias = "get_package_installation_directory_of_module")] |
| 61 | +pub fn package_installation_directory_of_module( |
| 62 | + hmodule: std::os::windows::raw::HANDLE, |
| 63 | +) -> Result<PathBuf, std::io::Error> { |
| 64 | + // # Safety |
| 65 | + // The underlying `GetModuleFilenameW` function has three possible |
| 66 | + // outcomes when a raw pointer get passed to it: |
| 67 | + // - When the pointer is a valid HINSTANCE of a DLL (e.g. acquired |
| 68 | + // through the `GetModuleHandleW`), it sets a file path to the |
| 69 | + // assigned "out" buffer and sets the return value to be the length |
| 70 | + // of said path string |
| 71 | + // - When the pointer is null, it sets the full path of the process' |
| 72 | + // executable binary to the assigned buffer and sets the return value |
| 73 | + // to be the length of said string |
| 74 | + // - Whenever the provided buffer size is too small, it will set a |
| 75 | + // truncated version of the path and return the length of said string |
| 76 | + // while also setting the thread-local last-error code to |
| 77 | + // `ERROR_INSUFFICIENT_BUFFER` (evaluates to 0x7A) |
| 78 | + // - When the pointer is not a valid HINSTANCE that isn't NULL (e.g. |
| 79 | + // a pointer to some GKeyFile), it will return 0 and set the last-error |
| 80 | + // code to `ERROR_MOD_NOT_FOUND` (evaluates to 0x7E) |
| 81 | + // |
| 82 | + // The `g_win32_get_package_installation_directory_of_module` already |
| 83 | + // handles all of the outcomes gracefully by: |
| 84 | + // - Preallocating a MAX_PATH-long array of wchar_t for the out buffer, |
| 85 | + // so that outcome #3 can be safely assumed to never happen |
| 86 | + // - Returning NULL when outcome #4 happens |
| 87 | + match unsafe { |
| 88 | + from_glib_full::<_, Option<PathBuf>>( |
| 89 | + ffi::g_win32_get_package_installation_directory_of_module(hmodule), |
| 90 | + ) |
| 91 | + } { |
| 92 | + Some(pb) => Ok(pb), |
| 93 | + None => Err(std::io::Error::last_os_error()), |
| 94 | + } |
| 95 | +} |
0 commit comments