# cpolyhook2
**Repository Path**: keyestore/cpolyhook2
## Basic Information
- **Project Name**: cpolyhook2
- **Description**: Simple C-API for PolyHook2.
- **Primary Language**: C#
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-07-28
- **Last Updated**: 2026-07-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README

# CPolyHook2
**`CPolyHook2` is a C-style wrapper library for [PolyHook2](https://github.com/stevemk14ebr/PolyHook_2_0), a pretty cool C++20, x86/x64 hooking library.**
| CI/CD | Release | Tech Stack | Platform | License |
|:---:|:---:|:---:|:---:|:---:|
| [](https://gitlab.com/Rawra/cpolyhook2/-/pipelines) | [](https://gitlab.com/Rawra/cpolyhook2/-/releases) |    |   | 
This project exposes PolyHook2's core C++ classes and functionality through a unmangled C-style API. The primary goal is to enable the use of PolyHook2 from other programming languages that can interface with C, such as C#, Python, Rust, and others, via P/Invoke or similar FFI mechanisms.
## Features
This library provides direct, `extern "C"` wrappers for major PolyHook2 components, including:
* **Detours:** `x86Detour`, `x64Detour`, `NatDetour`
* **PE Hooks:** `IatHook`, `EatHook`
* **Exception-Based Hooks:** `BreakPointHook`, `HWBreakPointHook`, `AVehHook`
* **Virtual Function/Table Hooks:** `VFuncSwapHook`, `VTableSwapHook`
* **Utilities:** `MemAccessor`, `ILCallback`, `RangeAllocator`, `ErrorLog` and more.
## Building cpolyhook2
This project uses a modern CMake setup and relies on the [vcpkg](https://github.com/microsoft/vcpkg) C++ package manager to handle dependencies. The following instructions are for a Windows environment. Although the process supports Linux-x64 as well.
### Prerequisites
Before you begin, ensure you have the following tools installed and configured:
1. **CMake**: Version 3.20 or newer. You can download it from the [official CMake website](https://cmake.org/download/).
2. **Visual Studio**: Visual Studio 2019 or newer is required. During installation, you must include the **"Desktop development with C++"** workload.
3. **vcpkg**: The C++ package manager from Microsoft.
* **Crucially**, set the `VCPKG_ROOT` environment variable to point to your vcpkg installation directory. This is required for CMake to automatically find the vcpkg toolchain.
4. **Git**: Required to clone this repository.
### Build Instructions
You can build the project using the command line or directly within Visual Studio.
#### Option 1: Command Line (Recommended)
1. **Clone the repository:**
```bash
git clone https://gitlab.com/Rawra/cpolyhook2.git
cd cpolyhook2
```
2. **Configure the project with CMake:**
Run the following command from the root of the repository. This will generate the build files in a `build` subdirectory. The `-DCMAKE_TOOLCHAIN_FILE` argument tells CMake to use vcpkg to find and link the PolyHook2 dependency.
```bash
# The %VCPKG_ROOT% variable should point to your vcpkg installation
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake"
```
3. **Compile the library:**
Use CMake's build driver to compile the project. You can specify the configuration (Debug or Release).
* **To build in Release mode:**
```bash
cmake --build build --config Release
```
* **To build in Debug mode:**
```bash
cmake --build build --config Debug
```
4. **Build the project:**
You can now build directly from the Visual Studio menu through the generated project files.
### Build Output
After a successful build, you will find all the necessary files in the build output directory (e.g., `build/` for the command-line method, or `build/x64-Debug/` for the IDE method).
The output directory will contain:
* `cpolyhook2.dll`: The compiled C-API shared library.
* `cpolyhook2.lib`: The import library required for linking against the DLL.
* `cpolyhook2.pdb`: The debug symbols (for debug builds).
* **Dependency DLLs**: vcpkg automatically copies all required runtime dependencies (like `PolyHook_2.dll`, `Zydis.dll`, etc.) into this directory, making it a self-contained, ready-to-use package.
## Usage
To use this library from another language, load the compiled DLL and call the exported functions using your language's FFI capabilities. All exported functions are unmangled and use the `__cdecl` calling convention.
### Example (C# P/Invoke Signature)
```csharp
using System;
using System.Runtime.InteropServices;
public static class CPolyHook2
{
private const string DllName = "cpolyhook2.dll";
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ph2_x64Detour_create")]
public static extern IntPtr x64Detour_create(UInt64 fnAddress, UInt64 fnCallback, UInt64 userTrampVar);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ph2_x64Detour_hook")]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool x64Detour_hook(IntPtr self);
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ph2_x64Detour_destroy")]
public static extern void x64Detour_destroy(IntPtr self);
}
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.