Question

I am making a program in win32 c using visual studio rc and I can't figure out how to do this seemingly simple task. I have a static text control, an edit control, and a button. When the user clicks the button, I wan't the program to take the text in the edit control and add it to whatever is in the static text box. After much headache and various attempts, I still cannot get this to work. I can retrieve the text from the edit control fine, but any attempt to add it to the static control crashes. Well actually just to clarify, I can set the text fine; it's just adding to the existing text that crashes the program. Could someone please post some code that would allow me to do this using GetWindowText() and SetWindowText(), (or something else if it preferable). Here is what I have:

SendMessage(hwndEditControl, WM_GETTEXT,255,(LPARAM)editbuffer);
GetWindowText(hwndTextControl, (LPWSTR)allText, GetWindowTextLength(hwndTextControl));
//function to add data, please create on
SetWindowText(hwndTextControl, (LPCWSTR)allText);}
//where
static TCHAR*       editbuffer = new TCHAR; //feel free to change these declarations
static TCHAR*       allText    = new TCHAR;

Any help much appreciated! Thanks!

Was it helpful?

Solution

You're only reserving one char for your string buffers. So instead of

static TCHAR*       editbuffer = new TCHAR;
static TCHAR*       allText    = new TCHAR;

you have to reserve much more memory, e.g.:

static TCHAR*       editbuffer = new TCHAR[255];
static TCHAR*       allText    = new TCHAR[255];

which would give you string buffers that can hold 254 chars and the terminating null char.

However I would recommend that you simply reserve the memory on the stack inside your function:

void OnButtonClick()
{
    TCHAR editbuffer[255] = {0};
    TCHAR allText[255] = {0};
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top