我有代码:
......
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/