GetWindowTextW just first Character

Hey! Currently trying to read the Value / Caption from a button or Label. The only Problem: It just returns the first Character :-S Any idears to resolve?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wstring hGUI::CtrlGetData(int id)
{
    int length = SendMessage(mapObjectHandler[id],WM_GETTEXTLENGTH,0,0);

    if(length == -1)
        return L"";

    wchar_t* buffer = new wchar_t[length+1];
    GetWindowTextW(mapObjectHandler[id],buffer,length);
    std::wstring str(buffer);
    delete[] buffer;

    return str;
}
What happens when you set "length" to some large number like 256, just for diagnostic purposes? Are you sure your handles in your 'mapObjectHandler[]' array are valid?
Hey! if i set it to 256 its still just the first Character :-(. If i Use GetWindowTextA it returns the full string. But i need to handle it as wstring not as string :-S
My guess is that it is because you are trying to create an std::wstring by passing it a pointer to an array, then you are returning a single std::wstring.
But also if i'am using a normal std::string and cast it into a wstring there is also just the first character :-(
Wait hold on, I think I'm getting mixed up. Let me take another look at this.

EDIT: What happens if you try to echo output the contents of "buffer" from within the function?
Last edited on
I'd say verify that the values in your hwnd array are valid, or that you are not trying to access an element outside of the range of that array. Here's what I did since getting hwnd values for Windows can be a PITA, I hard coded the value from a valid Thread ID in Process Explorer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

#define UNICODE

#include <windows.h>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

BOOL CALLBACK WindowsProc(HWND hwnd, LPARAM lParam)
{
    int length = 256; //SendMessage(hwnd,WM_GETTEXTLENGTH,0,0);

    wchar_t* buffer = new wchar_t[length+1];

    GetWindowTextW(hwnd, buffer, length);
    std::wstring str(buffer);
    delete[] buffer;

    std::wcout << str << std::endl;

    return TRUE;
}



int main()
{
    EnumDesktopWindows(GetThreadDesktop(5524), WindowsProc, NULL); //HERE IS THE HARD CODED THREAD ID VALUE THAT YOU MAY NEED TO CHANGE
    
    pause();
    return 0;
}


This seems to work for me.

EDIT: I may be using the term "worked" too loosely. This will give the titles of threads that have open Windows along with a bunch of white space from threads that don't.
Last edited on
Hey! I'am not trying to get the Window catption, i want to get the buttonlabels text. the problem i don't understand:

if i use GetWindowTextA for an CreateWindowExW button... it will give me the full caption. But also if i try to convert the result into wstring it just gives me the first character :-(
How are you converting the string into a wide string?
Last edited on
std::wstring ConvertFromUtf8ToUtf16(const std::string& str)
{
std::wstring convertedString;
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);
if(requiredSize > 0)
{
std::vector<wchar_t> buffer(requiredSize);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], requiredSize);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}

return convertedString;
}
Why are you using a vector? Just do what you did in the other code sample and make a dynamically sized array.

EDIT: By the way, I tested your code without a vector and it works. I actually can't get it to compile with MSVC 2010 if I include the vector header.
Last edited on
i can't get more then the first Char with GetWindowTextW and with GetWindowTextA it works. But if you convert from ascii to wide then the new var also just owns the first character :-S. So there seems to be a problem between ascii and widestring -.-. Don't got a clue what could be wrong :-(
currently i use mingw. Maybe the compilation is the problem. do you know how to set the compiler to compile as unicode?
Yeah, #define UNICODE some where above your Windows headers.
Also don't help. :-S why does my programm handle the stuff as ascii if i'am just using widechar funktions o.O
Have you updated mingw lately? I've about given up on trying to use it with the WinAPI the default installation is just missing too much. There are off shoot mingw's that I hear work pretty well but I just run MSVC 2010 in Code::Blocks.
Topic archived. No new replies allowed.