GetWindowTextW just first Character

Nov 18, 2013 at 6:20pm
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;
}
Nov 18, 2013 at 6:35pm
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?
Nov 18, 2013 at 7:06pm
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
Nov 18, 2013 at 8:18pm
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.
Nov 18, 2013 at 8:59pm
But also if i'am using a normal std::string and cast it into a wstring there is also just the first character :-(
Nov 18, 2013 at 9:04pm
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 Nov 18, 2013 at 9:55pm
Nov 18, 2013 at 10:08pm
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 Nov 18, 2013 at 10:12pm
Nov 18, 2013 at 10:15pm
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 :-(
Nov 18, 2013 at 10:31pm
How are you converting the string into a wide string?
Last edited on Nov 18, 2013 at 10:34pm
Nov 18, 2013 at 10:49pm
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;
}
Nov 18, 2013 at 11:06pm
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 Nov 18, 2013 at 11:23pm
Nov 18, 2013 at 11:26pm
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 :-(
Nov 18, 2013 at 11:37pm
currently i use mingw. Maybe the compilation is the problem. do you know how to set the compiler to compile as unicode?
Nov 18, 2013 at 11:40pm
Yeah, #define UNICODE some where above your Windows headers.
Nov 18, 2013 at 11:44pm
Also don't help. :-S why does my programm handle the stuff as ascii if i'am just using widechar funktions o.O
Nov 18, 2013 at 11:52pm
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.