jp9000 46aeb46757 win-capture: Add hooking functions
These functions allow the safe hooking of windows functions,
specifically windows API functions that may or may not have built-in
machine code to help aid in reverse chain hooks.

If a new hook is applied to an existing forward hook, that hook will be
preserved to prevent that new hook's data from being removed
unintentionally.

Hopefully with all these precautions this will reduce the likelihood of
crashes and abnormal hook behavior, while allowing existing hooks to be
preserved, and allowing new hooks to be applied.
2014-12-09 14:21:03 -08:00

53 lines
1.1 KiB
C

#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#else
#ifndef inline
#define inline __inline
#endif
#endif
enum hook_type {
HOOKTYPE_FORWARD_OVERWRITE,
HOOKTYPE_FORWARD_CHAIN,
HOOKTYPE_REVERSE_CHAIN
};
struct func_hook {
void *call_addr;
uintptr_t func_addr; /* function being hooked to */
uintptr_t hook_addr; /* hook function itself */
const char *name;
enum hook_type type;
bool is_64bit_jump;
bool hooked;
bool started;
uint8_t unhook_data[14];
uint8_t rehook_data[14];
};
extern void hook_init(struct func_hook *hook,
void *func_addr, void *hook_addr, const char *name);
extern void hook_start(struct func_hook *hook);
extern void do_hook(struct func_hook *hook, bool force);
extern void unhook(struct func_hook *hook);
static inline void rehook(struct func_hook *hook)
{
do_hook(hook, false);
}
static inline void force_rehook(struct func_hook *hook)
{
do_hook(hook, true);
}
#ifdef __cplusplus
}
#endif