Thread: GetWindowText Wide Character Issue

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    26

    GetWindowText Wide Character Issue

    Hello,

    I am enumerating windows on windows. I get the HWND in enumerate function.

    If I use wide character method, I see console output only shows 3 windows titles:

    Code:
    	TCHAR* buff;
    	buff = new TCHAR[length + 1];
    	memset(buff, 0, (length + 1) * sizeof(TCHAR));
    	GetWindowText(hWnd, buff, length + 1);
    	tstring winTitle = tstring(buff);
    	delete[] buff;
    	const wchar_t* wTitle = winTitle .c_str();
    	wcout << TEXT("wTitle: ") << wTitle << std::endl;
    But if use simple string method, I see console outputs 18 windows titles:
    Code:
    	char buff[1026];
    	int res = GetWindowTextA(hWnd, buff, 1026);
    	printf(buff)
    What might be the issue with GetWindowText so that it prevents other 15 windows titles to be printed? How can I trace the issue with GetWindowText and wide characters?

    This is C++ console application in VS Community 2017.

    Cheers

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    This gives me a long list of windows titles.
    Code:
    BOOL CALLBACK EnumWindows(HWND hWnd, LPARAM /* lParam */)
    {
      const int BUF_SIZE = 1024;
      std::wstring buffer(BUF_SIZE, L' ');
      int len = GetWindowTextW(hWnd, &buffer[0], BUF_SIZE);
      if (len > 0)
      {
        buffer.resize(len);
        std::wcout << L"Window text: " << buffer << "\n";
      }
    
      return TRUE;
    }

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    26
    Thank you very much for your help.

    This works better. But somehow some of the Google Chrome windows are not retrieved by your code. What might be the issue?

    Especially if Google Chrome windows are minimized, the Window text: is not got.

    Cheers

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    Some windows don't have a text, so they don't get printed.
    A program doesn't have to set a text for the window

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  2. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. Where is my extended wide character ?
    By intmail in forum C Programming
    Replies: 4
    Last Post: 02-14-2006, 04:54 PM
  5. Wide Character Writing
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2005, 02:00 PM

Tags for this Thread