I am trying to make an API hook using Detour to extract the text output from a third party program. However, I only get rubbish, alot of numbers and no text output.
Exactly when are these functions called? Are they called to draw other stuff than text aswell? If there is some advanced tools the third party program uses to avoid intercepting these calls, is there some basic example I can try to make sure that my method really recieves the text correctly? In other words, is there some program in windows which uses these methods to draw text on the screen?
My code can be seen below:
BOOL (__stdcall *Real_ExtTextOut)(HDC hdc,int x, int y, UINT options, const RECT* lprc,LPCWSTR text,UINT cbCount, const INT* lpSpacingValues) = ExtTextOut;
BOOL (__stdcall *Real_DrawText)(HDC hdc, LPCWSTR text, int nCount, LPRECT lpRect, UINT uOptions) = DrawText;
int WINAPI Mine_DrawText(HDC hdc, LPCWSTR text, int nCount, LPRECT lpRect, UINT uOptions)
{
ofstream myFile;
myFile.open ("C:\\temp\\textHooking\\textHook\\example.txt", ios::app);
for(int i = 0; i < nCount; ++i)
myFile << text[i];
myFile << endl;
int rv = Real_DrawText(hdc, text, nCount, lpRect, uOptions);
return rv;
}
BOOL WINAPI Mine_ExtTextOut(HDC hdc, int X, int Y, UINT options, RECT* lprc, LPCWSTR text, UINT cbCount, INT* lpSpacingValues)
{
ofstream myFile;
myFile.open ("C:\\temp\\textHooking\\textHook\\example2.txt", ios::app);
for(int i = 0; i < cbCount; ++i)
myFile << text[i];
myFile << endl;
BOOL rv = Real_ExtTextOut(hdc, X, Y, options, lprc, text, cbCount, lpSpacingValues);
return rv;
}
// Install the DrawText detour whenever this DLL is loaded into any process
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved){
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut);
DetourAttach(&(PVOID&)Real_DrawText, Mine_DrawText);
DetourTransactionCommit();
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}