nt_user_call/
error.rs

1//! Provides error enumerations.
2
3use std::fmt::Display;
4
5use windows::{
6    core::HRESULT,
7    Win32::Foundation::{
8        ERROR_MOD_NOT_FOUND, ERROR_NOT_SUPPORTED, ERROR_OLD_WIN_VERSION, E_ILLEGAL_METHOD_CALL,
9    },
10};
11
12#[repr(usize)]
13#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
14pub enum UserCallError {
15    OsNotSupported = 1,
16    OsTooNew = 2,
17    CallNotFound = 3,
18    LibraryNotFound = 4,
19}
20
21impl Display for UserCallError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::OsNotSupported => write!(f, "The operating system is not supported."),
25            Self::OsTooNew => write!(
26                f,
27                "The operating system does not use the NtUserCall* family of syscalls anymore."
28            ),
29            Self::CallNotFound => write!(f, "The function was not found."),
30            Self::LibraryNotFound => write!(f, "A required library was not found."),
31        }
32    }
33}
34
35impl TryFrom<usize> for UserCallError {
36    type Error = ();
37
38    fn try_from(value: usize) -> Result<Self, Self::Error> {
39        match value {
40            1 => Ok(Self::OsNotSupported),
41            2 => Ok(Self::OsTooNew),
42            3 => Ok(Self::CallNotFound),
43            4 => Ok(Self::LibraryNotFound),
44            _ => Err(()),
45        }
46    }
47}
48
49impl From<UserCallError> for windows::core::Error {
50    fn from(value: UserCallError) -> Self {
51        match value {
52            UserCallError::OsNotSupported => {
53                Self::from_hresult(HRESULT::from_win32(ERROR_OLD_WIN_VERSION.0))
54            }
55            UserCallError::OsTooNew => Self::from_hresult(E_ILLEGAL_METHOD_CALL),
56            UserCallError::CallNotFound => {
57                Self::from_hresult(HRESULT::from_win32(ERROR_NOT_SUPPORTED.0))
58            }
59            UserCallError::LibraryNotFound => {
60                Self::from_hresult(HRESULT::from_win32(ERROR_MOD_NOT_FOUND.0))
61            }
62        }
63    }
64}