glib-win32 0.22.6

Rust bindings for the GLibWin32 library
Documentation
// Take a look at the license at the top of the repository in the LICENSE file.
#[cfg(any(windows, docsrs))]
use glib::translate::*;
#[cfg(any(windows, docsrs))]
use std::path::PathBuf;

#[cfg(any(windows, docsrs))]
use crate::ffi;

#[cfg(windows)]
use std::os::windows::raw::HANDLE;
#[cfg(all(unix, docsrs))]
pub type HANDLE = *mut std::os::raw::c_void;

#[doc(alias = "g_win32_get_package_installation_directory_of_module")]
#[doc(alias = "get_package_installation_directory_of_module")]
#[cfg(any(windows, docsrs))]
pub fn package_installation_directory_of_module(
    hmodule: HANDLE,
) -> Result<PathBuf, std::io::Error> {
    // # Safety
    // The underlying `GetModuleFilenameW` function has three possible
    // outcomes when a raw pointer get passed to it:
    // - When the pointer is a valid HINSTANCE of a DLL (e.g. acquired
    // through the `GetModuleHandleW`), it sets a file path to the
    // assigned "out" buffer and sets the return value to be the length
    // of said path string
    // - When the pointer is null, it sets the full path of the process'
    // executable binary to the assigned buffer and sets the return value
    // to be the length of said string
    // - Whenever the provided buffer size is too small, it will set a
    // truncated version of the path and return the length of said string
    // while also setting the thread-local last-error code to
    // `ERROR_INSUFFICIENT_BUFFER` (evaluates to 0x7A)
    // - When the pointer is not a valid HINSTANCE that isn't NULL (e.g.
    // a pointer to some GKeyFile), it will return 0 and set the last-error
    // code to `ERROR_MOD_NOT_FOUND` (evaluates to 0x7E)
    //
    // The `g_win32_get_package_installation_directory_of_module` already
    // handles all of the outcomes gracefully by:
    // - Preallocating a MAX_PATH-long array of wchar_t for the out buffer,
    // so that outcome #3 can be safely assumed to never happen
    // - Returning NULL when outcome #4 happens
    match unsafe {
        from_glib_full::<_, Option<PathBuf>>(
            ffi::g_win32_get_package_installation_directory_of_module(hmodule),
        )
    } {
        Some(pb) => Ok(pb),
        None => Err(std::io::Error::last_os_error()),
    }
}

#[doc(alias = "g_win32_get_package_installation_directory")]
#[doc(alias = "get_package_installation_directory")]
#[deprecated = "Use `package_installation_directory_of_module()`"]
#[cfg(any(windows, docsrs))]
pub fn package_installation_directory(package: &str, dll_name: &str) -> glib::GString {
    unsafe {
        from_glib_full(ffi::g_win32_get_package_installation_directory(
            package.to_glib_none().0,
            dll_name.to_glib_none().0,
        ))
    }
}

#[doc(alias = "g_win32_get_package_installation_subdirectory")]
#[doc(alias = "get_package_installation_subdirectory")]
#[deprecated = "Use `package_installation_directory_of_module()`"]
#[cfg(any(windows, docsrs))]
pub fn package_installation_subdirectory(
    package: &str,
    dll_name: &str,
    subdir: &str,
) -> glib::GString {
    unsafe {
        from_glib_full(ffi::g_win32_get_package_installation_subdirectory(
            package.to_glib_none().0,
            dll_name.to_glib_none().0,
            subdir.to_glib_none().0,
        ))
    }
}