Skip to main content

LM_UnhookCode

Removes a hook/detour placed on the address from, restoring it to its original state. The function also frees the trampoline allocated by LM_HookCode.

Signature

lm_bool_t LM_UnhookCode(
    lm_address_t from,
    lm_address_t trampoline,
    lm_size_t    size
);

Parameters

from
lm_address_t
required
The address where the hook was placed.
trampoline
lm_address_t
required
The address of the trampoline generated by LM_HookCode.
size
lm_size_t
required
The amount of bytes occupied by the hook (aligned to the nearest instruction). This is the value returned by LM_HookCode.

Returns

LM_TRUE on success, LM_FALSE on failure.

Example

#include <libmem/libmem.h>

int main()
{
    lm_address_t fn_target = 0x12345678;
    lm_address_t fn_hook = (lm_address_t)my_hook_function;
    lm_address_t trampoline;
    lm_size_t hook_size;

    // Place the hook
    hook_size = LM_HookCode(fn_target, fn_hook, &trampoline);
    if (hook_size > 0) {
        printf("[*] Hook placed successfully (%zu bytes)\n", hook_size);
    }

    // ... do something ...

    // Remove the hook
    if (LM_UnhookCode(fn_target, trampoline, hook_size)) {
        printf("[*] Hook removed successfully\n");
    }

    return 0;
}

LM_UnhookCodeEx

Removes a hook/detour placed on the address from in a remote process, restoring it to its original state. The function also frees the trampoline allocated by LM_HookCodeEx.

Signature

lm_bool_t LM_UnhookCodeEx(
    const lm_process_t *process,
    lm_address_t        from,
    lm_address_t        trampoline,
    lm_size_t           size
);

Parameters

process
const lm_process_t*
required
The remote process where the hook was placed.
from
lm_address_t
required
The address where the hook was placed in the remote process.
trampoline
lm_address_t
required
The address of the trampoline generated by LM_HookCodeEx.
size
lm_size_t
required
The amount of bytes occupied by the hook (aligned to the nearest instruction) in the remote process. This is the value returned by LM_HookCodeEx.

Returns

LM_TRUE on success, LM_FALSE on failure.

Example

#include <libmem/libmem.h>

int main()
{
    lm_process_t target_process;
    lm_address_t fn_target = 0x12345678;
    lm_address_t fn_hook = 0x87654321;
    lm_address_t trampoline;
    lm_size_t hook_size;

    // Find the target process
    if (!LM_FindProcess("game.exe", &target_process)) {
        printf("Failed to find process\n");
        return 1;
    }

    // Place the hook in the remote process
    hook_size = LM_HookCodeEx(&target_process, fn_target, fn_hook, &trampoline);
    if (hook_size > 0) {
        printf("[*] Hook placed successfully (%zu bytes)\n", hook_size);
    }

    // ... do something ...

    // Remove the hook from the remote process
    if (LM_UnhookCodeEx(&target_process, fn_target, trampoline, hook_size)) {
        printf("[*] Hook removed successfully\n");
    }

    return 0;
}

Build docs developers (and LLMs) love