【发布时间】:2023-04-07 18:37:02
【问题描述】:
我正在尝试使用 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;
}
【问题讨论】:
-
文件或屏幕上的垃圾?文件内容肯定会乱七八糟,它不能正确处理 Unicode。
-
我的意思是文件。有什么建议如何处理吗?
-
好吧,我会用谷歌搜索“c++ write utf-8 file”。前几首热门歌曲看起来不错。