I'm trying to use detours to hook text output for example in notepad.
I wrote the following code. I will not put here all code, but the most significant parts.
DLL part:
DLLEXPORT LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0) {
return CallNextHookEx(0, nCode, wParam, lParam);
}
if (nCode == HCBT_ACTIVATE)
{
HWND hWnd = (HWND)wParam;
TCHAR szTemp[255];
GetWindowText(hWnd, szTemp, 255);
DetourTransactionBegin();
DetourUpdateThread(hWnd);
DetourAttach(&(PVOID&)Real_DrawText, Mine_DrawText);
DetourTransactionCommit();
DetourTransactionBegin();
DetourUpdateThread(hWnd);
DetourAttach(&(PVOID&)Real_DrawTextEx, Mine_DrawTextEx);
DetourTransactionCommit();
DetourTransactionBegin();
DetourUpdateThread(hWnd);
DetourAttach(&(PVOID&)Real_TextOut, Mine_TextOut);
DetourTransactionCommit();
DetourTransactionBegin();
DetourUpdateThread(hWnd);
DetourAttach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut);
DetourTransactionCommit();
}
return 0;
}
Client part:
int main(int argc, char* argv[]) {
HOOKPROC hkprcSysMsg;
static HINSTANCE hinstDLL;
static HHOOK hhookSysMsg;
hinstDLL = LoadLibrary(TEXT("dllsample.dll"));
//cout << (hinstDLL == NULL);
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "_CBTProc@12");
DWORD dw = GetLastError();
//cout << (hkprcSysMsg == NULL);
//cout << dw;
hhookSysMsg = SetWindowsHookEx(
WH_CBT,
hkprcSysMsg,
hinstDLL,
0);
//std::cout << (hhookSysMsg == NULL);
int i;
std::cin >> i;
}
The problem is that all 4 functions which draw text are not hooked. What do I do wrong. I'v started studing detours and didn't find answer for my question in docs.
If other parts of code are required, I'll put them here later.
DrawText is a macro that goes to either DrawTextW or DrawTextA depending on the UNICODE preprocessor setting. So maybe notepad is calling one, and you are hooking the other?
I think DrawTextA forwards to DrawTextW, so try hooking that directly.
edit, sample code below, compile with commands at top. run main.exe. run sysinternals debug view to see the output.
The code compiles to a dll called t4.dll and an executable called main.exe, when you run main.exe, the dll is loaded into every running process by the SetWindowHookEx call, and then the CBTProc function is called on every thread at the appropriate time.