free not working with malloc
In trying to get the text from a list box, concatenate that with the url to dictionary.com, then look up the word useing ShellExecute, the following didn't go according to plan. It works perfectly the first time, but the second time URLbuf gets merged with the previous text. eg. looking up "emitt" -> URLbuf = "http://dictionary.reference.com/browse/emitt". But then trying to look up "emitter" -> URLbuf = "http://dictionary.reference.com/browse/emittemitter". Here is the code, with 2010 being the ID of the listbox:
Code:
char *URLbuf = (char*) malloc(50);
BOOL lResult = SendMessage(GetDlgItem(hwnd, 2010),LB_GETCURSEL,0,0);
if (lResult >= 0) {
SendMessage(GetDlgItem(hwnd, 2010),393,lResult,(WPARAM)URLbuf);
URLbuf = strncat("http://dictionary.reference.com/browse/", URLbuf,50);
ShellExecute(hwnd,"open",URLbuf, NULL, NULL, SW_SHOWNORMAL); }
free(URLbuf);
return 0;
break;
}
I have tryed setting *URLbuf = 0 or setting it to NULL, but that disables URLbuf from holding anymore strings. If anyone could effectively set URLbuf = "" so each time the buton is clicked and this code is executed, I wouldn't have to restart the application. BTW: this is for a target solver for the SMH (my second GUI application fully coded in C).