任何人都可以解释为什么cout后的语句会改变输出?

时间:2013-08-30 18:26:37

标签: windows cout

最奇怪的事情正在发生。在下面的程序中,我打印出一行。按原样,程序输出以下内容:

-----FOCUS-----:0x22fc80:

但是,如果我在cout之后注释掉7个语句中的任何一个,我会得到以下结果:

-----FOCUS-----:0:

我已经包含了tchar.h,windows.h,iostream和Psapi.h。我使用的是MinGW-64,编译命令是:

"C:\MinGW64\bin\g++.exe" -o MyProgram.exe MyProgram.cpp -lpsapi

该计划是:

int main()
{
    HWND focusThreadWnd = GetForegroundWindow();
    DWORD focusThreadID = GetWindowThreadProcessId(focusThreadWnd,0);
    GUITHREADINFO focusThreadInfo;
    GetGUIThreadInfo(focusThreadID, &focusThreadInfo);
    HWND focus = focusThreadInfo.hwndFocus;
    cout << "-----FOCUS-----:" << focus << ":" << endl;

    HWND k1Focus = focusThreadWnd;
    HWND k2Focus = focusThreadWnd;
    HWND k3Focus = focusThreadWnd;
    HWND k4Focus = focusThreadWnd;
    HWND k5Focus = focusThreadWnd;
    HWND k6Focus = focusThreadWnd;
    HWND k7Focus = focusThreadWnd;
}

所以我的问题是,cout后的命令如何影响输出?而且,为什么这七个命令中的存储会改变输出?而且,7有什么特别之处?

1 个答案:

答案 0 :(得分:1)

我没有重现您的问题,但根据GetGUIThreadInfo

的MSDN页面
Note that you must set the cbSize member to sizeof(GUITHREADINFO) before calling this function.

那些额外的7个HWND影响了你的堆栈帧,导致cbSize被不同的值填充