c++ - 无法使::WideCharToMultiByte 工作

标签 c++ detours

我有一个用于注入(inject)的 DLL。这是通过 CBT-hook 注入(inject)的。现在,当需要 通过 CBT 遇到进程,我已经绕过 WinAPI 的 ExtTextOutW 和我自己的。 ExtTextOutW 的规范是:


BOOL ExtTextOutW(HDC         hdc,
                 INT         x,
                 INT         y,
                 UINT        flags,
                 const RECT* lprect,
                 LPCWSTR     str,
                 UINT        count,
                 const INT*  lpDx)

在绕行的 ExtTextOutW 中,我尝试使用以下代码将 str (LPCWSTR) 转换为多字节:


BOOL Mine_ExtTextOutW(HDC         hdc,
                      INT         x,
                      INT         y,
                      UINT        flags,
                      const RECT* lprect,
                      LPCWSTR     str,
                      UINT        count,
                      const INT*  lpDx)
{
        BOOL rv = Real_ExtTextOutW(hdc, x, y, flags, lprect, str, count, lpDx);

        HANDLE h = ::WindowFromDC(hdc);

        if (!h || !str)
                return ev;

        CHAR *buffer = (CHAR *)::LocalAlloc(count + 1);

        int l = ::WideCharToMultiByte(CP_APC, 0, str, count, buffer, count, NULL, NULL);

        if (l > -1) {
                buffer[l] = '\0';

                g_pClient->SendViaIPC(buffer);
        }

        ::LocalFree(buffer);

        return rv;
}

不幸的是,这不起作用。 WideCharToMultiByte 挂起注入(inject)的进程。为什么?

最佳答案

你的代码看起来有点奇怪,它能通过编译吗?

LocalAlloc 应该有两个参数,你是说 CP_ACP 吗?无论如何,我会:

  • 向 WideCharToMultiByte 询问正确的大小,以防您将来更改代码页。
  • 检查 > 0(失败用 0 表示,而不是 -1)
  • 使用 std 字符串只是为了确保您没有任何内存泄漏、异常等。

所以像这样:

int nMultiByteSize = ::WideCharToMultiByte( CP_ACP, NULL, str, count, NULL, 0, NULL, NULL );
if ( nMultiByteSize > 0 )
{
    std:string strMulti;
    strMulti.resize( nMultiByteSize );

    if ( ::WideCharToMultiByte( CP_ACP, NULL, str, count, &strMulti[0], (int)strMulti.size(), NULL, NULL ) > 0)
    {
        g_pClient->SendViaIPC(strMulti.c_str());
    }
}

关于c++ - 无法使::WideCharToMultiByte 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1377843/

相关文章:

c - 通过绕道减少非决定论?

c++ - Microsoft Detours - 无法 Hook __thiscall 函数

c++ - Detours 3.0 Hook 崩溃 MessageBoxA

c++ - 将 std::strings 的 std::vector 解析为任意类型的 std::tuple

c++ - 保证内存排序和正确的编程实践

c++ - 在PROGMEM中添加更多数据会中断Arduino Mega 2560上的SPI传输

c++ - 绕行并使用 _thiscall 作为 Hook (GCC 调用约定)

c++ - openMP lastprivate 和 firstprivate 到同一个变量

c++ - 预期在 '[' token 和 + 之前有不合格的 id

c++ - 绕行函数在 printf 上崩溃