   SetWindowText() and strcat()
  Having picked up the basics of socket programming (with winsock) it is kind of frustrating to see that everything but the nice GUI in which you thought to present your work works.Before I get into all the flashy stuff I'd like to write a MUD and I'm currently trying to write the server. The server window has a big read-only outputfield in which I would like the servermessages to be displayed.However, I end up with a problem... I need to get the current text in the output first, in order to concatenate messages to it. As you can see, I do that in the code below, but then the concatenating comes, which is totally messed up. This might partly be because of my horrible pointer knowledge, but consider that I have tried every possible typecast or whatever to make it work. Sometimes I didn't even get errors, but then it would crash on execution.Can someone show me the proper way of doing the thing below?// This bit of code executes when pressing the 'start server' buttonint srvReturn = InitServer ( serversocket, WsaData, localAddress, (unsigned short)6000 );HWND hOutput = GetDlgItem ( hWnd, TXT_OUTPUT );char * outputtext;GetWindowText ( hOutput, outputtext, GetWindowTextLength ( hOutput ) );if ( srvReturn == 0 ) {    char * newtext = strcat ( outputtext, (const char *)"\n[server]: Could not initialize server environment!" );    SetWindowText ( hOutput, newtext );} else {    char * newtext = strcat ( outputtext, (const char *)"\n[server]: Server environment successfully initialized!" );SetWindowText ( hOutput, newtext );}
  You need to actually allocate a buffer for the text to be stored in.  If you're using C++ the best way to do this would be:// This bit of code executes when pressing the 'start server' buttonint srvReturn = InitServer ( serversocket, WsaData, localAddress, (unsigned short)6000 );HWND hOutput = GetDlgItem ( hWnd, TXT_OUTPUT );// get the minimum buffer size needed// the + 1 may not be neccessary (msdn docs are unclear), but ensures we have// room for a zero termination byte and eliminates the problem of a zero length// bufferstd::size_t bufferSize = GetWindowTextLength(hOutput) + 1;// allocate a bufferstd::vector< char > buffer(bufferSize);// read into the buffer;GetWindowText( hOutput, &buffer[0], buffer.size() );if ( srvReturn == 0 ) {std::string message = std::string(&buffer[0]) + "\n[server]: Could not initialize server environment!";SetWindowText ( hOutput, message.c_str() );} else {std::string message = std::string(&buffer[0]) + "\n[server]: Server environment successfully initialized!";SetWindowText ( hOutput, message.c_str() );}If you're using plain C then try:// This bit of code executes when pressing the 'start server' buttonint srvReturn = InitServer ( serversocket, WsaData, localAddress, (unsigned short)6000 );HWND hOutput = GetDlgItem ( hWnd, TXT_OUTPUT );// get the minimum buffer size needed// the + 1 may not be neccessary (msdn docs are unclear), but ensures we have// room for a zero termination bytestd::size_t bufferSize = GetWindowTextLength(hOutput) + 1;// allocate a bufferchar * buffer = malloc(bufferSize * sizeof(char));if (!buffer){// handle error}// read into the buffer;GetWindowText( hOutput, buffer, buffer.size() );if ( srvReturn == 0 ) {// allocate a new buffer big enough for the additional textchar * message = malloc(bufferSize + strlen("\n[server]: Could not initialize server environment!"));if (!message){// handle error}strcpy(message, buffer);strcat(message, "\n[server]: Could not initialize server environment!");free(buffer);SetWindowText ( hOutput, message );free(message);} else {// allocate a new buffer big enough for the additional textchar * message = malloc(bufferSize + strlen("\n[server]: Server environment successfully initialized!"));if (!message){// handle error}strcpy(message, buffer);strcat(message, "\n[server]: Server environment successfully initialized!");free(buffer);SetWindowText ( hOutput, message );free(message);}The C code could be better, but it should give you an idea.Σnigma   ;  Thanks a lot for that reply, Enigma. The C-style suggestion did it, only had to add a few typecasts to satisfy the compiler but it worked great after that ;)   ;  Just a sugestion, instead of allocating memory yourself you can utilize messages such as EM_SETSEL and EM_REPLACESEL to simply append data to your edit control.I wrote a quick example to show what I mean:void AppendData(HWND pTargetHandle, std::string const & rData, bool bNewLine = true){if(!pTargetHandle || rData.empty())return void();LRESULT iDummy = SendMessage(pTargetHandle, WM_GETTEXTLENGTH, NULL, NULL);if(iDummy + static_cast <LRESULT> (rData.size()) > SendMessage(pTargetHandle, EM_GETLIMITTEXT, NULL, NULL))SendMessage(pTargetHandle, EM_LIMITTEXT, iDummy + rData.size(), NULL);SendMessage(pTargetHandle, EM_SETSEL, iDummy, iDummy);SendMessage(pTargetHandle, EM_REPLACESEL, FALSE, reinterpret_cast <LPARAM> (rData.c_str()));if(bNewLine)AppendData(pTargetHandle, "\r\n", false);}I think by default, the character limit of an edit control is about 30,000 characters, so if we run out of space it will allocate some more to fit your data.From what I understand you wanted something like this.Good luck!P.S.I move the location of the cursor each call because if you create your control with ES_READONLY flag the user is able to select data for copying, and if data is still selected when you append your new data, the selection will obviously be replaced with new data... So if you're using WS_DISABLED you can remove the call provided that you're starting to append data to an empty control, if not, then you only need to call it once.[Edited by - raz0r on May 27, 2006 6:34:07 PM];  Why thanks for your reply, raz0r, I experimented a bit with the given examples and they worked great for me.I have yet another question however which has to do with the character buffers in the reply from Enigma.I want to add a system time display to each line I add to the editcontrol, so it'd look like:[21:18:59]: the text here...EDIT: I solved it myself, it turned out that sprintf() was my big friend here :D[Edited by - rogierpennink on May 28, 2006 5:05:20 PM]