Skip to main content

minhook_detours_rs/error/
mod.rs

1//! Errors for `minhook-detours-sys`.
2
3use minhook_detours_sys::{
4    MH_ERROR_ALREADY_CREATED, MH_ERROR_ALREADY_INITIALIZED, MH_ERROR_DETOURS_TRANSACTION_BEGIN,
5    MH_ERROR_DETOURS_TRANSACTION_COMMIT, MH_ERROR_DISABLED, MH_ERROR_ENABLED,
6    MH_ERROR_FUNCTION_NOT_FOUND, MH_ERROR_MEMORY_ALLOC, MH_ERROR_MODULE_NOT_FOUND,
7    MH_ERROR_NOT_CREATED, MH_ERROR_NOT_EXECUTABLE, MH_ERROR_NOT_INITIALIZED,
8    MH_ERROR_UNABLE_TO_UNINITIALIZE, MH_ERROR_UNSUPPORTED_FUNCTION, MH_STATUS,
9};
10use thiserror::Error;
11
12#[derive(Debug, Error)]
13pub enum Error {
14    #[error("MinHook is already initialized")]
15    AlreadyInitialized,
16    #[error("MinHook is not initialized yet, or already uninitialized")]
17    NotInitialized,
18    #[error("MinHook can't be uninitialized due to hooks that failed to be removed")]
19    UnableToInitialize,
20    #[error("The hook for the specified target function is already created")]
21    AlreadyCreated,
22    #[error("The hook for the specified target function is not created yet")]
23    NotCreated,
24    #[error("The hook for the specified target function is already enabled")]
25    Enabled,
26    #[error("The hook for the specified target function is not enabled yet, or already disabled")]
27    Disabled,
28    #[error(
29        "The specified pointer is invalid. It points the address of non-allocated and/or non-executable region"
30    )]
31    NotExecutable,
32    #[error("Detours failed to begin the hooking transaction")]
33    FailedTransactionBegin,
34    #[error("Detours failed to commit the hooking transaction")]
35    FailedTransactionCommit,
36    #[error("The specified target function cannot be hooked")]
37    UnsupportedFunction,
38    #[error("Failed to allocate memory")]
39    FailedAllocatingMemory,
40    #[error("The specified module is not loaded")]
41    ModuleNotFound,
42    #[error("The specified function is not found")]
43    FunctionNotFound,
44
45    // -------------------------------------------------------------------------------------------------------
46    // Above are the MinHook-native possible errors, following are Rust-level ones. For consistency, even if
47    // it may fit, the previous results will not be used.
48    // -------------------------------------------------------------------------------------------------------
49    #[error("The specified pointer is known to be invalid")]
50    InvalidTarget,
51}
52
53impl From<MH_STATUS> for Error {
54    /// Implement [`Error`] from [`MH_STATUS`].
55    ///
56    /// # Arguments
57    ///
58    /// * `value` - [`MH_STATUS`] returned by C API.
59    fn from(value: MH_STATUS) -> Self {
60        match value {
61            MH_ERROR_ALREADY_INITIALIZED => Self::AlreadyInitialized,
62            MH_ERROR_NOT_INITIALIZED => Self::NotInitialized,
63            MH_ERROR_UNABLE_TO_UNINITIALIZE => Self::UnableToInitialize,
64            MH_ERROR_ALREADY_CREATED => Self::AlreadyCreated,
65            MH_ERROR_NOT_CREATED => Self::NotCreated,
66            MH_ERROR_ENABLED => Self::Enabled,
67            MH_ERROR_DISABLED => Self::Disabled,
68            MH_ERROR_NOT_EXECUTABLE => Self::NotExecutable,
69            MH_ERROR_DETOURS_TRANSACTION_BEGIN => Self::FailedTransactionBegin,
70            MH_ERROR_DETOURS_TRANSACTION_COMMIT => Self::FailedTransactionCommit,
71            MH_ERROR_UNSUPPORTED_FUNCTION => Self::UnsupportedFunction,
72            MH_ERROR_MEMORY_ALLOC => Self::FailedAllocatingMemory,
73            MH_ERROR_MODULE_NOT_FOUND => Self::ModuleNotFound,
74            MH_ERROR_FUNCTION_NOT_FOUND => Self::FunctionNotFound,
75            _ => unreachable!(),
76        }
77    }
78}
79
80pub type Result<T> = std::result::Result<T, Error>;