diff --git a/include/radhook/api.h b/include/radhook/api.h index ff14786..9051f64 100644 --- a/include/radhook/api.h +++ b/include/radhook/api.h @@ -42,6 +42,7 @@ enum class RadHookResult : int { InvalidHandle, TrampolineTooFar, TargetTooSmall, + UnknownTarget, Unknown, }; @@ -412,4 +413,46 @@ RADHOOK_API RadHookResult RadHookApplyQueued(); +_Success_(return == RadHookResult::Success) +/** + * @brief Finds a hook handle from a specified target. + * + * @param target Target. + * @param outHandle Receives the hook handle on success. + * + * @return Operation result. + */ +RADHOOK_API +RadHookResult +RadHookGetHandleFromTarget( + _In_ void* target, + _Out_ RadHookHandle* outHandle + ); + +_Success_(return == RadHookResult::Success) +/** + * @brief Finds a hook handle from a specified target function pointer. + * + * Convenience overload that accepts a typed function pointer for the target. + * + * @tparam TargetFn Target function pointer type. + * @param target Target function. + * @param outHandle Receives the hook handle on success. + * + * @return Operation result. + */ +template +requires std::is_function_v> +RadHookResult +RadHookGetHandleFromTarget( + TargetFn target, + _Out_ RadHookHandle* outHandle + ) +{ + return RadHookGetHandleFromTarget( + reinterpret_cast(target), + outHandle + ); +} + #endif // __LIBRADHOOK_API_H__ \ No newline at end of file diff --git a/include/radhook/defines.h b/include/radhook/defines.h index 691510e..0000364 100644 --- a/include/radhook/defines.h +++ b/include/radhook/defines.h @@ -30,10 +30,8 @@ #if defined(_WIN32) # if defined(RADHOOK_BUILD) # define RADHOOK_API __declspec(dllexport) -# elif defined(RADHOOK_SHARED) -# define RADHOOK_API __declspec(dllimport) # else -# define RADHOOK_API +# define RADHOOK_API __declspec(dllimport) # endif #else # if defined(__GNUC__) || defined(__clang__) diff --git a/src/radhook.cpp b/src/radhook.cpp index e0c5943..60ed5db 100644 --- a/src/radhook.cpp +++ b/src/radhook.cpp @@ -41,6 +41,7 @@ const char* RadHookResultToString(RadHookResult result) { case RadHookResult::InvalidHandle: return "InvalidHandle"; case RadHookResult::TrampolineTooFar: return "TrampolineTooFar"; case RadHookResult::TargetTooSmall: return "TargetTooSmall"; + case RadHookResult::UnknownTarget: return "UnknownTarget"; default: return "Unknown"; } } @@ -519,4 +520,15 @@ RadHookResult RadHookApplyQueued() { } } return last; +} + +RadHookResult RadHookGetHandleFromTarget(void* target, RadHookHandle* outHandle) { + for (auto const& h : g_hooks) { + auto p = h.get(); + if (p->target == target) { + *outHandle = p; + return RadHookResult::Success; + } + } + return RadHookResult::UnknownTarget; } \ No newline at end of file