Can't get RICHEDIT content into WCHAR*

Hello again.
I have changed some code from ASCII to UNICODE so I have to use WCHAR instead of CHAR. What I am trying to do is to get the content of a RICHEDIT control into a WCHAR pointer for further use. In the ASCII version (where I use CHAR), it does work, but with WCHAR I am getting pointers troubles. My code is this:
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
38
39
40
41
42
43
44
45
46
// Global variables
HWND            RichEditControl;
ULONG           FileLength; // Length of the file
WCHAR           FileContent; // Pointer for the content to be stored
GETTEXTLENGTHEX GetLengthMode; // GETTEXTLENGTHEX structure
GETTEXTEX       GetTextMode; // GETTEXTEX structure

// WM_CREATE message
case WM_CREATE:
     // Create the rich edit control and other controls...
     
     GetLengthMode.flags = GTL_DEFAULT;
     GetLengthMode.codepage = 1200; // UNICODE CODEPAGE

     GetTextMode.flags = GT_DEFAULT;
     GetTextMode.codepage = 1200; // UNICODE CODEPAGE
     GetTextMode.lpDefaultChar = NULL;
     GetTextMode.lpUsedDefChar = NULL;
break;

// WM_COMMAND message, when I press some button to get the text
case WM_COMMAND:
     switch(LOWORD(Wparam)) {
          case BTN_GETTEXT:
               // I get the length of the RichEditControl content correctly
               FileLength = SendMessage(
                                        RichEditControl, 
                                        EM_GETTEXTLENGTHEX, 
                                        LPARAM(&GetLengthMode, 0)
                                        );
               // If RichEditControl not empty
               if (FileLength>0) {
                    FileContent = new WCHAR[FileLength];
                    GetTextMode.cb = FileLength;
                    SendMessage(
                                RichEditControl, 
                                EM_GETTEXTEX, 
                                WPARAM(&GetTextMode), 
                                LPARAM(&FileContent)
                                );

                    MessageBox(NULL, FileContent, L"RichEditControl Content", MB_OK);
               }
          break;
     }
break;


When I click on BTN_GETTEXT I get a message box with no text, so I don't understand why I cannot get the text.
Some clues? Thanks!
Last edited on
Everything looks ok, except for line 34. FileLength is the length in characters, but it has to be converted to bytes. You have to multiply by sizeof(WCHAR). Other than that it should work, I guess.
If I do this:
1
2
FileContent = new WCHAR[FileLength * sizeof(WCHAR)];
GetTextMode.cb = FileLength * sizeof(WCHAR);

the application crashes when I click on BTN_GETTEXT.

If I do this:
1
2
FileContent = new WCHAR[FileLength * 2];
GetTextMode.cb = FileLength;

I get weird characters
Last edited on
Topic archived. No new replies allowed.