我有一个用于注入(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/