Unknow String length

This is a discussion on Unknow String length within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to get all the text from an edit box using the GetWindowTExt command but since the text in ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    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?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    int GetWindowTextLength(
    HWND hWnd // handle to window or control with text
    );

    it returns the length in characters.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    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.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    int len = GetWindowTextLen(ebox);
    char *buffer= new char[len+1];
    GetWindowText(ebox,buffer,len);

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    when your done with the buffer use delete on it or you will get a memory leak

    delete[] buffer;
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    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;

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You never allocate memory for *added.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    you can use that function once and on the second time you try it it crashes

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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;
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    yeah, thats exactally what i have down

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    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.

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    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
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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