Displaying chat messages

This is a discussion on Displaying chat messages within the Windows Programming forums, part of the Platform Specific Boards category; hi all, i have figure out how to display chat messages in a edit box BUT the buffer that i ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Angry Displaying chat messages

    hi all,
    i have figure out how to display chat messages in a edit box BUT the buffer that i use gets full and then when ever somert is put in the buffer it over writes what is at the end i need to know how to increase the size of the buffer so it can hold the new messages with out a buffer limit so i can carry on writing messages to the screen in my chat prog .. like msn

    void OutputTextToScreen(char *p,int error)
    {

    GetWindowText(hMessage, szNewBuffer, 100);

    strcat(szNewBuffer, p);
    if((error != -1 ) || (error != SOCKET_ERROR)){
    strcat(szNewBuffer, "OK\r\n");
    }else{
    strcat(szNewBuffer, "Failed\r\n");
    }
    SetWindowText(hMessage, szNewBuffer);
    }
    Theres a sucker born every minute, but a swallower is harder to find

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    did this with [ code ] and [ /code ] without the spaces.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    look up realloc()

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Maybe a edit is the wrong control (it is the easiest to use and most common).

    A listview (or Listbox) could have each line of chat added as an item, retrieved with one of the many Listview_ series of macros, eliminating the need to GlobalRealloc() (or LocalRealloc() ) the buffer.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Question ermm.....

    hi every time i try to run my progy its crashs every time i try to use the function - OutputTextToLog



    please tell me if i am doing anything wrong this is the code :-

    Code:
    void OutputTextToLog(char *p,int error)
    {
         len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGEAREA));
         GetWindowText(hMessage, szNewBuffer, 256);
         char* szBuffer2;
         realloc(szBuffer2, len+256);
         strcat(szBuffer2,szNewBuffer);
         strcat(szBuffer2,p);
         if((error != -1 ) || (error != SOCKET_ERROR)){
         strcat(szBuffer2, "OK\r\n");
         }else{
         strcat(szBuffer2, "Failed\r\n");
         }
         SetWindowText(hMessage, szBuffer2);
    }
    thx in advance
    Last edited by poopy_pants2; 08-23-2002 at 01:16 PM.
    Theres a sucker born every minute, but a swallower is harder to find

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Is hMessage the same as GetDlgItem(hwnd, IDC_MESSAGEAREA)? If it is, try this:

    Code:
    len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGEAREA));
    GetWindowText(hMessage, szNewBuffer, 256);
    
    replaced by:
    
    len = GetDlgItemText(hwnd, IDC_MESSAGEAREA, szNewBuffer, 256);
    Note that GetWindowTextLength returns the size of the text, while GetDlgItemText() returns the number of characters copied to szNewBuffer. This probably won't solve any problems, but it's neater and easier to understand (assuming that this is what you really wanted to do, of course ).

    You can also do this:
    Code:
    SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);


    Code:
    szBuffer2 = realloc(szBuffer2, len+256);   //<-------
    strcat(szBuffer2,szNewBuffer);
    strcat(szBuffer2,p);
    if((error != -1 ) || (error != SOCKET_ERROR)){
    strcat(szBuffer2, "OK\r\n");
    }else{
    strcat(szBuffer2, "Failed\r\n");
    }
    You are assuming that p is at max 247 characters?
    Last edited by Hunter2; 08-23-2002 at 01:21 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Angry dam...

    right what u told me did clear things up BUT still it continues to crash i think i have found the prob its not the realloc its the

    Code:
    strcat(szBuffer2,szNewBuffer);
    because it doesnt get to the second messagebox before it crashs if wont let me put any thing in the szBuffer2 and heres the new code -

    Code:
    int nMessageLen;
    int nPLen;
    char szNewBuffer[1024];
    char szBuffer[100] = "Ufo Text Messenger v1.00\r\n\r\n";
    char* szBuffer2;
    
    void OutputTextToLog(char *p,int error)
    {
         nMessageLen = GetDlgItemText(hwnd, IDC_MESSAGEAREA, szNewBuffer, 1024);
         nPLen = strlen(p);
    
         realloc(szBuffer2, nMessageLen+nPLen+10);
         MessageBox(0,"1",0,0);
         strcat(szBuffer2,szNewBuffer);
         MessageBox(0,"2",0,0);
         strcat(szBuffer2,p);
         if((error != -1 ) || (error != SOCKET_ERROR)){
         strcat(szBuffer2, "OK\r\n");
         }else{
         strcat(szBuffer2, "Failed\r\n");
         }
         SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);
    }
    PLEASE HELP THIS HAS BEEN DRIVING ME MAD!!!!!!!!!!!! MAD!!!!!!!!!!!! AND ITS SLOWING ME DOWN

    thx in advance
    Last edited by poopy_pants2; 08-23-2002 at 02:40 PM.
    Theres a sucker born every minute, but a swallower is harder to find

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It gave you an illegal operation, right? If it did, did you click on details? I think the problem was realloc(). strcat() shouldn't crash unless you didn't reserve enough memory or something... try this instead:

    Code:
    szBuffer2 = (char*)malloc(nMessageLen + nPLen + 10);
    //other stuff
    SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);
    
    free((void*)szBuffer2); //you might not need the typecast
    I really have no idea if it will work (I use C++, so I use new and delete instead of malloc() and free()), but you can give it a try. If it still doesn't work, try replacing "malloc(nMessageLen + nPLen + 10);" with "malloc(2000)". If it works then, then you know that there's something wrong with "nMessageLen + nPLen + 10", and work on the problem from there.

    Good luck!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh, I totally forgot: You can use a std::string. I'll mark the changes with comments.
    PHP Code:
    //int nMessageLen;   //<----
    //int nPLen;   //<----
    char szNewBuffer[1024];
    char szBuffer[100] = "Ufo Text Messenger v1.00\r\n\r\n";
    std::string szBuffer2;    //or rename it if you want

    void OutputTextToLog(char *p,int error)
    {
         
    GetDlgItemText(hwndIDC_MESSAGEAREAszNewBuffer1024);   //<----
         //nPLen = strlen(p);   //<----

         //realloc(szBuffer2, nMessageLen+nPLen+10);   //<----
         
    MessageBox(0,"1",0,0);
         
    szBuffer2.append(szNewBuffer);   //<----
         
    MessageBox(0,"2",0,0);
         
    szBuffer2.append(p);   //<----
         
    if((error != -) || (error != SOCKET_ERROR)){
         
    szBuffer2.append("OK\r\n");   //<----
         
    }else{
         
    szBuffer2.append("Failed\r\n");   //<----
         
    }
         
    SetDlgItemText(hwndIDC_MESSAGEAREAszBuffer2.c_str());   //<----

    szBuffer2 is not a c-style string (i.e. isn't a char*), so in order to use it in SetDlgItemText(), you have to call its c_str() function. But remember, you CANNOT use the code below, because c_str() returns a constant string.
    PHP Code:
    GetDlgItemText(hwndIDC_MESSAGEAREAszBuffer2.c_str(), 1024); 
    Well, good luck!
    Last edited by Hunter2; 08-23-2002 at 02:57 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Talking YAAHOO!!!!!!!

    THANKS ALOT hunter YOUR DA MAN

    THIS GUY KNOWS HIS STUFF IT WORKED FIRST TIME HAHAHAHAHAHA!!!!!!!!!!!
    Theres a sucker born every minute, but a swallower is harder to find

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No prob
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spy++ view messages posted/sent to a control, or from it?
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2007, 11:07 PM
  2. Sending windows messages
    By Ideswa in forum Windows Programming
    Replies: 2
    Last Post: 03-02-2006, 12:27 PM
  3. edit box affecting displaying of text?
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2005, 03:28 PM
  4. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 12:57 PM
  5. Rough Portable Chat Design Sketch
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2001, 07:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21