简体   繁体   中英

Unable to get the window title

I am unable to get the window title of the Skype. The following code returns an empty string:

std::wstring get_window_title(HWND hwnd)
{
  wchar_t window_title[256];
  SendMessageW(hwnd, WM_GETTEXT, (WPARAM)256, (LPARAM)window_title);
  return window_title;
}

Window's handle is valid.

Am I using this function incorrectly?

Thanks in advance.

I tested your code and could not reproduce the issue. The issue is not in the piece of code you have shown, rather in the acquiring of the window handle. I suggest we vote to close this post, and you can ask a new question regarding the window handle.

For the record, I tested both with SendMessage and GetWindowText .

Your code actually works fine, modulo the fact that you don't check for errors. You need to check the value returned by SendMessageW . This return value indicates how many characters were copied to your buffer.

The most likely explanation for the code to fail when you run it is that you are supplying an invalid or incorrect window handle.

在这种情况下,我似乎需要使用GlobalAlloc函数。

Try GetWindowText , which can get around some of the difficulties of trying to send a message to a window in another process.

WM_GETTEXT asks the other process to write to a buffer that's in your address space rather than its own, which is not feasible. A few messages will marshal such buffers between process address spaces, but I don't think WM_GETTEXT is one of them.

UPDATE : It's been suggested in the comments that WM_GETTEXT is one of the messages that will marshal a string between process address spaces. I haven't found an authoritative source for that, and have found many non-authoritative posts claiming both that it does and does not. Regardless, GetWindowText is documented to work for non-child windows in other processes, so it's still a viable solution to the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM