c++ - 错误 1400 无效的窗口句柄

标签 c++

我有代码:

......
    g_hWnd = CreateWindow( L"Urok6WindowClass", L"TitleWindow", WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
                           NULL );
    if( !g_hWnd )
        return E_FAIL;

    ShowWindow(g_hWnd, nCmdShow );

一切正常。但是如果我添加:

    LPTSTR tilte_old= L"Old title";
    int gettus = GetWindowText(g_hWnd, tilte_old, 100);
    DWORD error = GetLastError();
    WCHAR szTest[10];
    swprintf_s(szTest, 10, L"%d", error); 
    MessageBox(NULL, szTest, L"TEST2", MB_OK); 

消息框 >> 1400

得到了>>''

为什么会这样?为什么windows句柄无效?

最佳答案

second parameter GetWindowText 的是一个输出参数:

lpString [out] Type: LPTSTR

The buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a null character.

所以你需要提供一个要填充的缓冲区,而不是一个指向常量字符串的指针(第三个参数表示缓冲区的最大大小以避免溢出)

尝试:

const size_t BUFF_SIZE = 100;
TCHAR title_old[BUFF_SIZE];
int gettus = GetWindowText(g_hWnd, title_old, BUFF_SIZE);

此外,如果 gettus 为 0(GetWindowText() 失败),您只需调用 GetLastError

关于c++ - 错误 1400 无效的窗口句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138626/

相关文章:

c++ - 转换运算符重载以返回 2-dim 数组以访问外部库

c++ - std::string 实现是否符合 's.c_str() + s.size()' 不一定与 '&s[s.size()]' 相同?

c++ - 可以检索 C++11 中线程函数的返回值吗?

c++ - 计算新行、制表符和空格

c++ - 按点坐标旋转点 vector

c++ - 使用 gcc4.2 在 OsX 10.9 上编译 Qt5.2 项目?

c++ - 如何在 C++ 中获取所有可能的 n 个字符的字符串?

c++ - 为什么我得到 Present table dump for device[1] : NVIDIA Tesla GPU 0 in this code?

c++ - 如何在OpenCV中使用网络摄像头获取Mat图像的像素值

c++ - 编译错误 'set_ssl_context_callback' : on Windows