Note that there are some explanatory texts on larger screens.

plurals
  1. PODetouring a member-function via an injected DLL
    primarykey
    data
    text
    <p><em><strong>Original Post:</em></strong></p> <p>I'm trying to detour a member-function from within my injected DLL. I've got the address of the function I'm trying to hook, but cannot figure out the proper syntax or way about hooking it via detours library. I've commented the line that's giving me the error with the error message.</p> <p>I've read the source for the detours example of member-function hooking and that's what this code is based upon, but for some reason it's not working.</p> <p>Any help would be much appreciated, thanks!</p> <pre><code>#include &lt;windows.h&gt; #include &lt;detours.h&gt; class CDetour { public: bool My_MemFn(unsigned int unk1); static bool (CDetour::* Real_MemFn)(unsigned int); }; bool CDetour::My_MemFn(unsigned int unk1) { /* do stuff here */ return (this-&gt;*Real_MemFn)(unk1); } typedef bool (CDetour::* MemFn_t)(unsigned int unk1); MemFn_t CDetour::Real_MemFn= *(MemFn_t *)((void*)0x23234545); BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH: { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&amp;(PVOID&amp;)CDetour::Real_MemFn, *(PBYTE*)&amp;CDetour::My_MemFn); // ERROR: C2440: 'type cast' : cannot convert from 'bool __thiscall CDetour::* )(unsigned int)' to 'PBYTE *' DetourTransactionCommit(); break; } } return TRUE; } </code></pre> <p><em><strong>Solution:</em></strong></p> <pre><code>#include &lt;windows.h&gt; #include &lt;detours.h&gt; typedef void (__thiscall * CClassFunction_t)(void *__this, unsigned int unk1); CClassFunction_t Real_CClassFunction; void __fastcall Mine_CClassFunction(void *__this, int edx, unsigned int unk1) { Real_CClassFunction(__this, unk1); } template&lt;typename T&gt; void HookFunction(const char *module, char *signature, T &amp;fn_real, PVOID fn_mine) { HookFunction&lt;T&gt;(DetourFindFunction(module, signature), fn_real, fn_mine); } template&lt;typename T&gt; void HookFunction(DWORD address, T &amp;fn_real, PVOID fn_mine) { HookFunction&lt;T&gt;(reinterpret_cast&lt;PVOID&gt;(address), fn_real, fn_mine); } template&lt;typename T&gt; void HookFunction(PVOID target, T &amp;fn_real, PVOID fn_mine) { fn_real = reinterpret_cast&lt;T&gt;(target); HookFunction&lt;T&gt;(fn_real, fn_mine); } template&lt;typename T&gt; void HookFunction(T &amp;fn_real, PVOID fn_mine) { DetourAttach(&amp;(PVOID&amp;)fn_real, fn_mine); } void ApplyHooks() { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DWORD function_address = 0x12345678; HookFunction&lt;CClassFunction_t&gt;(address, Real_CClassFunction, Mine_CClassFunction); DetourTransactionCommit(); } BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: { DisableThreadLibraryCalls(hInstance); CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ApplyHooks, 0, 0, 0); break; } } return TRUE; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload