|
| 1 | +/** |
| 2 | +* ============================================================================= |
| 3 | +* DynamicHooks |
| 4 | +* Copyright (C) 2015 Robin Gohmert. All rights reserved. |
| 5 | +* ============================================================================= |
| 6 | +* |
| 7 | +* This software is provided 'as-is', without any express or implied warranty. |
| 8 | +* In no event will the authors be held liable for any damages arising from |
| 9 | +* the use of this software. |
| 10 | +* |
| 11 | +* Permission is granted to anyone to use this software for any purpose, |
| 12 | +* including commercial applications, and to alter it and redistribute it |
| 13 | +* freely, subject to the following restrictions: |
| 14 | +* |
| 15 | +* 1. The origin of this software must not be misrepresented; you must not |
| 16 | +* claim that you wrote the original software. If you use this software in a |
| 17 | +* product, an acknowledgment in the product documentation would be |
| 18 | +* appreciated but is not required. |
| 19 | +* |
| 20 | +* 2. Altered source versions must be plainly marked as such, and must not be |
| 21 | +* misrepresented as being the original software. |
| 22 | +* |
| 23 | +* 3. This notice may not be removed or altered from any source distribution. |
| 24 | +* |
| 25 | +* asm.h/cpp from devmaster.net (thanks cybermind) edited by pRED* to handle gcc |
| 26 | +* -fPIC thunks correctly |
| 27 | +* |
| 28 | +* Idea and trampoline code taken from DynDetours (thanks your-name-here). |
| 29 | +*/ |
| 30 | + |
| 31 | +// ============================================================================ |
| 32 | +// >> INCLUDES |
| 33 | +// ============================================================================ |
| 34 | +#include "assert.h" |
| 35 | + |
| 36 | +#include "manager.h" |
| 37 | +#include "conventions/x86GccThiscall.h" |
| 38 | + |
| 39 | + |
| 40 | +// ============================================================================ |
| 41 | +// >> GLOBAL VARIABLES |
| 42 | +// ============================================================================ |
| 43 | +int g_iMyFuncCallCount = 0; |
| 44 | +int g_iPreMyFuncCallCount = 0; |
| 45 | +int g_iPostMyFuncCallCount = 0; |
| 46 | + |
| 47 | + |
| 48 | +// ============================================================================ |
| 49 | +// >> cdecl test |
| 50 | +// ============================================================================ |
| 51 | +class MyClass; |
| 52 | +MyClass* g_pMyClass = NULL; |
| 53 | + |
| 54 | +class MyClass |
| 55 | +{ |
| 56 | +public: |
| 57 | + int MyFunc(int x, int y) |
| 58 | + { |
| 59 | + g_iMyFuncCallCount++; |
| 60 | + assert(this == g_pMyClass); |
| 61 | + assert(x == 3); |
| 62 | + assert(y == 10); |
| 63 | + |
| 64 | + int result = x + y; |
| 65 | + assert(result == 13); |
| 66 | + |
| 67 | + return result; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +bool PreMyFunc(HookType_t eHookType, CHook* pHook) |
| 72 | +{ |
| 73 | + g_iPreMyFuncCallCount++; |
| 74 | + MyClass* pMyClass = pHook->GetArgument<MyClass *>(0); |
| 75 | + assert(pMyClass == g_pMyClass); |
| 76 | + |
| 77 | + int x = pHook->GetArgument<int>(1); |
| 78 | + assert(x == 3); |
| 79 | + |
| 80 | + int y = pHook->GetArgument<int>(2); |
| 81 | + assert(y == 10); |
| 82 | + return false; |
| 83 | +} |
| 84 | + |
| 85 | +bool PostMyFunc(HookType_t eHookType, CHook* pHook) |
| 86 | +{ |
| 87 | + g_iPostMyFuncCallCount++; |
| 88 | + int x = pHook->GetArgument<int>(1); |
| 89 | + assert(x == 3); |
| 90 | + |
| 91 | + int y = pHook->GetArgument<int>(2); |
| 92 | + assert(y == 10); |
| 93 | + |
| 94 | + int return_value = pHook->GetReturnValue<int>(); |
| 95 | + assert(return_value == 13); |
| 96 | + |
| 97 | + pHook->SetReturnValue<int>(1337); |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +// ============================================================================ |
| 103 | +// >> main |
| 104 | +// ============================================================================ |
| 105 | +int main() |
| 106 | +{ |
| 107 | + CHookManager* pHookMngr = GetHookManager(); |
| 108 | + |
| 109 | + int (MyClass::*MyFunc)(int, int) = &MyClass::MyFunc; |
| 110 | + |
| 111 | + // Prepare calling convention |
| 112 | + std::vector<DataType_t> vecArgTypes; |
| 113 | + vecArgTypes.push_back(DATA_TYPE_POINTER); |
| 114 | + vecArgTypes.push_back(DATA_TYPE_INT); |
| 115 | + vecArgTypes.push_back(DATA_TYPE_INT); |
| 116 | + |
| 117 | + // Hook the function |
| 118 | + CHook* pHook = pHookMngr->HookFunction( |
| 119 | + (void *&) MyFunc, |
| 120 | + new x86GccThiscall(vecArgTypes, DATA_TYPE_INT) |
| 121 | + ); |
| 122 | + |
| 123 | + pHook->AddCallback(HOOKTYPE_PRE, (HookHandlerFn *) (void *) &PreMyFunc); |
| 124 | + pHook->AddCallback(HOOKTYPE_POST, (HookHandlerFn *) (void *) &PostMyFunc); |
| 125 | + |
| 126 | + // Call the function |
| 127 | + MyClass a; |
| 128 | + g_pMyClass = &a; |
| 129 | + |
| 130 | + int return_value = a.MyFunc(3, 10); |
| 131 | + |
| 132 | + assert(g_iMyFuncCallCount == 1); |
| 133 | + assert(g_iPreMyFuncCallCount == 1); |
| 134 | + assert(g_iPostMyFuncCallCount == 1); |
| 135 | + assert(return_value == 1337); |
| 136 | + |
| 137 | + pHookMngr->UnhookAllFunctions(); |
| 138 | + return 0; |
| 139 | +} |
0 commit comments