本文介绍了错误1400窗口句柄无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

......
    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 );

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

All works fine. But if I add:

    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);

MessageBox >> 1400

MessageBox >> 1400

gettus> >''

gettus >> ''

为什么会发生这种情况?为什么Windows句柄无效?

Why is this happening? Why windows handle is invalid?

推荐答案

的/library/windows/desktop/ms633520.aspxrel =nofollow>第二个参数是一个out参数:

The second parameter of GetWindowText is an out parameter:

将接收文本的缓冲区。如果字符串长度大于或长于缓冲区,则字符串将被截断并以空字符结束。

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.

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

So you need to supply a buffer to be filled, not a pointer to a constant string (the third parameter indicates the maximum size of the buffer to avoid overflow)

尝试:

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

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

Also, you only need to call GetLastError if gettus is 0 (GetWindowText() failed)

这篇关于错误1400窗口句柄无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:46
查看更多