Skip to main content
libmem provides both a C API (libmem.h) and a modern C++ API (libmem.hpp) with support for C++20 features like std::optional and type-safe wrappers.

Installation

Add the following to your CMakeLists.txt to fetch and configure libmem:
include(FetchContent)

# Download and set up libmem
FetchContent_Declare(libmem-config URL "https://raw.githubusercontent.com/rdbo/libmem/config-v1/libmem-config.cmake" DOWNLOAD_NO_EXTRACT TRUE)
FetchContent_MakeAvailable(libmem-config)
set(CMAKE_PREFIX_PATH "${libmem-config_SOURCE_DIR}" "${CMAKE_PREFIX_PATH}")
set(LIBMEM_DOWNLOAD_VERSION "5.1.4")

# Find libmem package
find_package(libmem CONFIG REQUIRED)
Then link against libmem:
# Link against libmem
target_link_libraries(<YOUR_TARGET_NAME> PRIVATE libmem::libmem)

Using vcpkg

vcpkg install libmem

Manual Installation

Prerequisites:Build and Install:
git clone --recursive --depth 1 https://github.com/rdbo/libmem
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
nmake
nmake install  # Run as Administrator

Header Files

#include <libmem/libmem.h>
The C API uses:
  • lm_bool_t for return values (LM_TRUE/LM_FALSE)
  • Structs like lm_process_t, lm_module_t, lm_address_t
  • Output parameters (pointers) for results
  • Manual memory management

Linking

target_link_libraries(your_target PRIVATE libmem::libmem)

Platform-Specific Dependencies

Windows:
  • user32.lib, psapi.lib, ntdll.lib, shell32.lib
Linux:
  • -ldl, -lstdc++, -lm
FreeBSD:
  • -ldl, -lkvm, -lprocstat, -lelf, -lstdc++, -lm

Basic Usage

#include <libmem/libmem.h>
#include <stdio.h>

int main()
{
    lm_module_t game_mod;
    lm_address_t fn_take_damage;

    // Find a module by name
    if (!LM_FindModule("game.dll", &game_mod)) {
        printf("Failed to find module\n");
        return 1;
    }
    printf("[*] Base address of 'game.dll': %p\n", (void *)game_mod.base);

    // Find a symbol in the module
    fn_take_damage = LM_FindSymbolAddress(&game_mod, "take_damage");
    if (fn_take_damage == LM_ADDRESS_BAD) {
        printf("Failed to find symbol\n");
        return 1;
    }
    printf("[*] Found 'take_damage' function: %p\n", (void *)fn_take_damage);

    return 0;
}
Key C API Functions:
FunctionDescriptionReturn Value
LM_FindProcess(name, &proc)Find process by namelm_bool_t
LM_FindModule(name, &mod)Find module by namelm_bool_t
LM_ReadMemory(src, dest, size)Read memorybytes read
LM_WriteMemory(dest, src, size)Write memorybytes written
LM_SigScan(sig, addr, size)Scan for signaturelm_address_t
LM_HookCode(from, to, &tramp)Hook functionhook size

Complete Example

#include <libmem/libmem.h>
#include <stdio.h>

void hk_take_damage(int amount)
{
    printf("hooked take_damage! no damage will be taken\n");
    return;
}

int main()
{
    lm_module_t game_mod;
    lm_address_t fn_take_damage;

    // Find the game module
    if (!LM_FindModule("game.dll", &game_mod)) {
        printf("Failed to find module\n");
        return 1;
    }
    printf("[*] Base address of 'game.dll': %p\n", (void *)game_mod.base);

    // Find the symbol to hook
    fn_take_damage = LM_FindSymbolAddress(&game_mod, "take_damage");
    if (fn_take_damage == LM_ADDRESS_BAD) {
        printf("Failed to find symbol\n");
        return 1;
    }
    printf("[*] Found 'take_damage' function: %p\n", (void *)fn_take_damage);

    // Hook the function
    if (!LM_HookCode(fn_take_damage, (lm_address_t)hk_take_damage, LM_NULLPTR)) {
        printf("Failed to hook function\n");
        return 1;
    }
    printf("[*] 'take_damage' hooked, player will no longer receive damage\n");

    return 0;
}

Type Reference

Core Types:
typedef uintptr_t lm_address_t;
typedef size_t lm_size_t;
typedef uint32_t lm_pid_t;
typedef uint32_t lm_prot_t;
Structures:
typedef struct lm_process_t {
    lm_pid_t  pid;
    lm_pid_t  ppid;
    lm_arch_t arch;
    lm_size_t bits;
    lm_time_t start_time;
    lm_char_t path[LM_PATH_MAX];
    lm_char_t name[LM_PATH_MAX];
} lm_process_t;

typedef struct lm_module_t {
    lm_address_t base;
    lm_address_t end;
    lm_size_t    size;
    lm_char_t    path[LM_PATH_MAX];
    lm_char_t    name[LM_PATH_MAX];
} lm_module_t;

typedef struct lm_inst_t {
    lm_address_t address;
    lm_size_t    size;
    lm_byte_t    bytes[LM_INST_MAX];
    lm_char_t    mnemonic[32];
    lm_char_t    op_str[160];
} lm_inst_t;
Constants:
#define LM_NULL    (0)
#define LM_NULLPTR ((void *)LM_NULL)
#define LM_ADDRESS_BAD ((lm_address_t)-1)
#define LM_PID_BAD ((lm_pid_t)-1)

See Also

Build docs developers (and LLMs) love