I have injected my DLL into a target application where I’ve hooked few WINAPI-functions
as well. One of them is DrawTextExW. I’m trying to replace all ‘l’ letters to ‘!’ before
it prints it out. My solution works fine for a few seconds, but then the target application crashes. I really don’t understand why.
Here’s the function:
Edit – Working solution:
int WINAPI DetouredDrawTextExW(__in HDC hdc,
__inout LPWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT dwDTFormat,
__in LPDRAWTEXTPARAMS lpDTParams)
{
std::wstring s_wc(lpchText, cchText);
std::replace(s_wc.begin(), s_wc.end(), L'l', L'!');
return ::DrawTextExW(hdc, const_cast<wchar_t *>(s_wc.c_str()),
s_wc.length(), lprc, dwDTFormat, lpDTParams);
}
So, can somebody point it out to me what I’m doing wrong?
I see that you ignore
cchText, could you be receiving an non-NULL-terminated string with a positive value forcchText, resulting in reading past the end of the string into invalid memory? That error would present as a Win32 exception in the constructor ofs_wc, though.Also, you aren’t checking for
DT_MODIFYSTRINGin thedwDTFormatparameter. If that flag is present, then ::DrawTextExW() could be overwriting invalid memory. That would present as a Win32 exception in ::DrawTextExW() or perhaps as a C++ exception in thes_wcdestructor.edit
Here’s uncompiled, untested code that I believe obeys the contract of
::DrawTextExW()