Strange behavior with GetWindowText

Nov 28, 2011 at 8:38pm
Hi, this is my first post here, English is not my primary language so sorry for possible mistakes.

I have been working around this since this morning and now I am asking for a little of your help

I am trying to get the text content of an EDIT control and store it into a LPTSTR global variable. What is strange is that I can get the length of the text inside the EDIT control but not the text itself. So this is my code:

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
LPTSTR dec;
HWND hwndEdit1;
int len1;
'
'
'WinMain and other irrelevant stuff for this issue
'
'
'
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
     switch (message) {
        case WM_CREATE:
             hWndEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT", "", 
WS_VISIBLE|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL,
5,30,585,150,hWnd, HMENU(5), hInstance, 0);
        break;
        case WM_COMMAND:
            if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)) {
                switch(LOWORD(wParam)) {
                     case 1:
                          len1 = GetWindowTextLength(hwndChild1);
                          GetWindowText(hWndEdit1, dec, len1);
                     break;
                }
     }
}


As I said, it get me the length of the EDIT right but the dec variable remains empty after GetWindowText. I tryied getting the EDIT content with SendMessage(WM_GETTEXT) but the application crashes. With SendMessageTimeout the result is the same as with GetWindowText: dec remains empty.

I am a C++ beginner and I just got in love with this programming language. For the sake of this "love story" give me some clues (I don't suppose getting a text from an EDIT is something so difficult). Thank you so much in advance!
I won't sleep until I get this solved.
Last edited on Nov 28, 2011 at 8:58pm
Nov 28, 2011 at 8:48pm
You need to pass the address of a buffer, not just a pointer that points no where.

Either switch to a buffer

TCHAR dec[256]; // or some suitable size

then you need to make sure len1 is less that 256

Or allocate a buffer using new

dec = new TCHAR[len1 + 1];

in this case you must remember to delete the memory you allocated later.
Last edited on Nov 28, 2011 at 8:48pm
Nov 28, 2011 at 8:52pm
PS It's easier to read code when very long lines are folded like this (or similarly):

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
LPTSTR dec;
HWND hwndEdit1;
int len1;
'
'
'WinMain and other irrelevant stuff for this issue
'
'
'
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
     switch (message) {
        case WM_CREATE:
             hWndEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT", "",
                                        WS_VISIBLE|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL,
                                        5,30,585,150,hWnd, HMENU(5), hInstance, 0);
        break;
        case WM_COMMAND:
            if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)) {
                switch(LOWORD(wParam)) {
                     case 1:
                          len1 = GetWindowTextLength(hwndChild1);
                          GetWindowText(hWndEdit1, dec, len1);
                     break;
                }
     }
}


Some people split calls like CreateWindowEx over even more lines.

It not only helps people with small laptop screens, like me. But when you've got the debugger open at the same time as your app, it make debugging easier.

Andy
Last edited on Nov 28, 2011 at 8:55pm
Nov 28, 2011 at 8:55pm
Thank you so much! Pointers, uuuuuuuuuuh. I missed that class.
Nov 28, 2011 at 8:57pm
Thanks for the organizing tips too. For whatever reason I don't like to split a command into more lines but I will keep your advice in mind for when I'll post code in this forum.
Thanks again!
Nov 28, 2011 at 9:43pm
And as for your English, its better than mine! And I'm English (american)!
Topic archived. No new replies allowed.