Results 1 to 3 of 3

Thread: Get Text from Edit

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Get Text from Edit

    Both work, and both seem to return a value, which is more correct:
    PHP Code:
    //First version -- as you see with one line commented out
    //Second version -- include the memory allocation line

    //--------------------
    //Get text from edit box
    //--------------------
    charGetTextFromEdit(HWND hwndDWORD ID)
    {
        
    charText;

        
    int len GetWindowTextLengthGetDlgItem(hwndID) );
        if(
    len 0)
        {
            
    //Text = (char*)GlobalAlloc(GPTR, len + 1);
            
    GetDlgItemText(hwndIDTextlen 1);
        }

        return 
    Text;

    Problem with the second version, is the function returns the text I need, I do not get a chance to unallocate it...Which is a problem, but it seems without allocating the memory, it works fine when returning to a char*
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    The first version is incorrect and your program will crash at some point - you're not allocated any memory for the text. GetDlgItemText, as far as I can see, does not allocate the memory for the text string itself - it expects a preallocated buffer and the length of that buffer to be passed to it. Therefore you *must* allocate the memory for the buffer yourself.

    Why use GlobalAlloc anyway? new is fine.

    As for freeing the memory later, you could just make the caller responsible for freeing it, or maybe use a smart pointer (though unless you're going to use smart pointers widely, I wouldn't use one here). You could just modify the function to put the returned string into an std::string and then free the buffer before returning the std::string..there's lots of ways you can approach it.
    an ending

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I would use std::string to make it easier on yourself:
    Code:
    string GetTextFromEdit(HWND hwnd, DWORD ID) 
    { 
        string str;
        LPTSTR buffer;
        int len = GetWindowTextLength(GetDlgItem(hwnd, ID)); 
    
        if(len > 0) 
        { 
            buffer = new TCHAR[len + 1];
            GetDlgItemText(hwnd, ID, buffer, len + 1); 
            str = buffer;
            delete[] buffer;
        } 
    
        return str; 
    }
    Then you can do something like MessageBox(hwnd, GetTextFromEdit(hwnd, ID).c_str(), "", 0);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width