Basic API hooking using detours

Below is a basic example on how to use the detours library to hook APIs.

#include <stdio.h>
#include <windows.h>
#include <detours.h>

// API that we want to hook 
DWORD (WINAPI * Real_SleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx;

// This function will be called *before* the API.
// We will modify one of the parameters 
// and then call the real API
static DWORD WINAPI Catch_SleepEx(DWORD dwMilliseconds, BOOL bAlertable)
{
    printf("SleepEx: %d\n", dwMilliseconds);
    dwMilliseconds = 10;
    printf("SleepEx: new value = %d\n", dwMilliseconds);    
    return Real_SleepEx(dwMilliseconds, bAlertable);
}

int main(int argc, char **argv)
{
    (void)argc;
    (void)argv;
 
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)Real_SleepEx, Catch_SleepEx);
    DetourTransactionCommit();

    SleepEx(5000, 0);

    return 0;
}

Enjoy!