【发布时间】:2021-11-07 02:06:11
【问题描述】:
下面的代码在我用来挂钩CreateWindowExA() 和其他函数的DLL 中。
当我使用std::wstringstream 将变量的值打印到DebugView 时,当前挂钩的应用程序崩溃。我确认当我评论 std::wstringstream 时它不会崩溃。
我可以使用哪些其他选项来打印不需要指定每个变量类型的值,就像您在 wsprintf() 上所做的那样?
HWND __stdcall CreateWindowExA_Hook(
DWORD dwExStyle,
LPCSTR lpClassName,
LPCSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
)
{
std::wstringstream text;
text << L"dwExStyle: " << dwExStyle << L" lpClassName: " << lpClassName << L" lpWindowName: " << lpWindowName
<< L" dwStyle: " << dwStyle << L" X: " << X << L" Y: " << Y << L" nWidth: " << nWidth << L" nHeight: " << nHeight
<< L" hWndParent: " << hWndParent << L" hMenu" << hMenu << L" hInstance: " << hInstance << L" lpParam: " << lpParam;
OutputDebugString(L"CreateWindowExA:");
OutputDebugString(text.str().c_str());
OutputDebugString(L" ");
return CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}
我想知道是否有比这样做“更好”的方法:
std::wstringstream ss;
( dwExStyle ? ss << L"dwExStyle: " << dwExStyle : ss << 0 );
( lpClassName ? ss << L" lpClassName: " << lpClassName : ss << 0 );
( lpWindowName ? ss << L" lpWindowName: " << lpWindowName : ss << 0 );
( dwStyle ? ss << L" dwStyle: " << dwStyle : ss << 0 );
....
OutputDebugString(L"CreateWindowExA:");
OutputDebugString(text.str().c_str());
OutputDebugString(L" ");
【问题讨论】:
-
是不是因为其中一个字符串为NULL?
-
可怕的想法:如果 bug 距离打印 5000 行代码怎么办?未知且遥远的错误对程序造成了致命的伤害,但程序在最终跌倒并死在无辜的某个地方之前蹒跚了一段时间?
-
尝试将打印出来的项目一一注释掉,看看是哪一个(或更多)导致它崩溃。
-
@user253751 很可能,我可以做些什么来避免在这个用例中打印一个 NULL 字符串? dratenik 我一直在使用它,不会停止检查每个值,因为它总是会有不同的情况。
-
(the_string == NULL ? L"(null)" : the_string)