【问题标题】:Why does API hooking ExtTextOut and DrawText in C++ only output rubbish?为什么 API 在 C++ 中挂钩 ExtTextOut 和 DrawText 只输出垃圾?
【发布时间】: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”。前几首热门歌曲看起来不错。

标签: c++ hook detours


【解决方案1】:

您正在将 UTF-16 字符代码写为整数。因此文件充满了数字。将文本缓冲区直接传输到文件可能更容易:

ofstream myFile;
myFile.open("C:\\temp\\textHooking\\textHook\\example.txt", ios::app);
myFile.write(reinterpret_cast<const char*>text, nCount*sizeof(*text));
myFile << endl;

您可能希望将 UTF-16LE BOM 放在文件的前面,以帮助您的文本编辑器计算出正在使用的编码。

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2021-02-20
    • 2018-09-12
    • 1970-01-01
    • 2018-02-18
    • 2020-12-17
    相关资源
    最近更新 更多