-
Unknow String length
I'm trying to get all the text from an edit box using the GetWindowTExt command but since the text in the edit box varies i don't want to pre set a string length incase it starts to run long, how would i determine how long the string would be in an edit box, would i use the strlen command?
-
int GetWindowTextLength(
HWND hWnd // handle to window or control with text
);
it returns the length in characters.
-
I figured out how to get the length of the string but it brings 1 more problem here what i have for code
int len = GetWindowTextLen(ebox);
char buffer; //(what do i put here since the length is undetermined)
GetWindowText(ebox,buffer,len);
i need to know what to do with the char buffer, i've tried char buffer[len] but it says constant expression required.
-
int len = GetWindowTextLen(ebox);
char *buffer= new char[len+1];
GetWindowText(ebox,buffer,len);
-
when your done with the buffer use delete on it or you will get a memory leak
delete[] buffer;
-
Now every time i use that function it crashes my program, heres what i have do you see anything wrong with it?
int len = GetWindowTextLength(ebox);
char *buffer= new char[len+1];
char *added;
GetWindowText(ebox,buffer,len);
sprintf(added,"%s\r\nADDED\r\n",buffer);
SetWindowText(ebox,added);
delete[] buffer;
delete[] added;
-
You never allocate memory for *added.
-
you can use that function once and on the second time you try it it crashes
-
did you allocate memory for added like so?
int len = GetWindowTextLength(ebox);
char *buffer = new char[len+1];
char *added = new char[len+1];
GetWindowText(ebox,buffer,len);
sprintf(added,"%s\r\nADDED\r\n",buffer);
SetWindowText(ebox,added);
delete[] buffer;
delete[] added;
-
yeah, thats exactally what i have down
-
Try safe deleting the memory by checking if it exists:
Code:
if(buffer)
{
delete[] buffer;
}
if(added)
{
delete[] added;
}
Also sometimes removing the "[]" will help.
-
Ummm..
Code:
int len = GetWindowTextLength(ebox);
char *buffer = new char[len+1];
char *added = new char[len+1]; //alloc size as lstrlen(buffer)
GetWindowText(ebox,buffer,len);
sprintf(added,"%s\r\nADDED\r\n",buffer); //then print lstrlen(buffer) + lstrlen("\r\nADDED\r\n") into *added (which is > lstrlen(buffer))
SetWindowText(ebox,added);
delete[] buffer;
delete[] added;
IMHO you should check each memory alloc no matter how small