aboutsummaryrefslogtreecommitdiffhomepage
path: root/ext/detours/samples/echo/echofx.cpp
blob: 73b5a7f363d0aca5e38f7e5501c625f8a983cd33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
//
//
#include <windows.h>
#include <detours.h>
#include <stdio.h>

int WINAPI Echo(PCSTR pszMsg);

static int (WINAPI * Real_Echo)(PCSTR pszMsg) = Echo;

int WINAPI Mine_Echo(PCSTR pszMsg)
{
    printf("Echo(%s)\n", pszMsg);
    return Real_Echo(pszMsg);
}

BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
{
    LONG error;
    (void)hinst;
    (void)reserved;

    if (DetourIsHelperProcess()) {
        return TRUE;
    }

    if (dwReason == DLL_PROCESS_ATTACH) {
        DetourRestoreAfterWith();

        printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
               " Starting.\n");
        fflush(stdout);

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)Real_Echo, Mine_Echo);
        error = DetourTransactionCommit();

        if (error == NO_ERROR) {
            printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
                   " Detoured Echo().\n");
        }
        else {
            printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
                   " Error detouring Echo(): %ld\n", error);
        }
    }
    else if (dwReason == DLL_PROCESS_DETACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)Real_Echo, Mine_Echo);
        error = DetourTransactionCommit();

        printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
               " Removed Echo() (result=%ld)\n", error);
        fflush(stdout);
    }
    return TRUE;
}