我正在尝试使用 Detour 制作一个 API 挂钩,以从第三方程序中提取文本输出。但是,我只得到垃圾,很多数字,没有文本输出。
究竟什么时候调用这些函数?他们是否也被要求画除文字以外的其他东西吗?如果第三方程序使用一些高级工具来避免拦截这些调用,是否有一些基本示例我可以尝试确保我的方法真正正确接收文本?换句话说,windows中是否有一些程序使用这些方法在屏幕上绘制文本?
我的代码如下所示:
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;
}