Thread: Copy strings from editcontrols

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    Copy strings from editcontrols

    Why doesent this work? All I get in my messagebox is... no nothing.

    Code:
    // Get the length of the line
    int length=SendMessage(hwndDlgIP,EM_LINELENGTH,0,0);
    
    //Make ready the buffer
    char *buffer=new char [length+1];
    
    //Add the terminating zero
    buffer[length+1]='\0';
    
    //Copy the string
    SendMessage(hwndDlgIP,EM_GETLINE,0,(LPARAM)buffer);
    
    //Show it
    MessageBox(NULL,buffer,"test",MB_OK);
    
    //Dont forget to delete trash
    delete buffer;

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Is there any text in the actual edit control? Is length >= 0?

    Is the hwndDlgIP a valid HWND? Try doing a GetDlgItem(hwnd,EDIT_CONTROLID) for a valid hwnd value.

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Yes. I tested to MessageBox-out the length variable, and it worked.

    Which means; I do have a valid HWND, and I do have a length...
    So what could the problem be?

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Try calling GetLastError() after each SendMessage. Check the return values for any possible problems with the two SendMessages.

    Remember HWNDs can be valid for the window, but if you havent retrienved the correct Dialog Controls handle - the send message wont appear to work.

    How have you determined the hwnd for this control? Please print this code.

    Where abouts are you doing this piece of code? inside the WM_COMMAND message loop? Print the whole section/function so i can easily see what is going wrong.
    Last edited by ventolin; 10-30-2005 at 04:19 PM.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Excerpt from MSDN:

    EM_GETLINE Message

    The EM_GETLINE message copies a line of text from an edit control and places it in a specified buffer. You can send this message to either an edit control or a rich edit control.

    Syntax

    To send this message, call the SendMessage function as follows.

    lResult = SendMessage( // returns LRESULT in lResult
    (HWND) hWndControl, // handle to destination control
    (UINT) EM_GETLINE, // message ID
    (WPARAM) wParam, // = (WPARAM) () wParam;
    (LPARAM) lParam // = (LPARAM) (LPCTSTR) lParam;
    );

    Parameters

    wParam
    Specifies the zero-based index of the line to retrieve from a multiline edit control. A value of zero specifies the topmost line. This parameter is ignored by a single-line edit control.

    lParam
    Pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line.

    Return Value

    The return value is the number of TCHARs copied. The return value is zero if the line number specified by the wParam parameter is greater than the number of lines in the edit control.

    Remarks

    Edit controls: The copied line does not contain a terminating null character.

    Rich edit controls: Supported in Microsoft Rich Edit 1.0 and later. The copied line does not contain a terminating null character, unless no text was copied. If no text was copied, the message places a null character at the beginning of the buffer. For information about the compatibility of rich edit versions with the various system versions, see About Rich Edit Controls.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Code:
    Before sending the message, set the first word of this
    buffer to the size, in TCHARs, of the buffer. For ANSI text, this is 
    the number of bytes; for Unicode text,
     this is the number of characters. The size in the first word is
    overwritten by the copied line.
    Yeah, thats probably it! ...though i didnt understand what they ment... Can someone show a examplecode?
    Last edited by Drogin; 10-31-2005 at 04:03 AM.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Wouldn't it be easier to just use GetWindowText?
    Or are you sure you want just one line?

  8. #8
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I am sure i just want one line..

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So you have a multiline edit control?

  10. #10
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    yeap.

  11. #11
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    So, what im after is how to initalize the buffer, so that the first WORD, is the length you wan to copy?

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    // Get the length of the line (truncated to a WORD)...
    WORD length = (WORD) SendMessage(hwndDlgIP, EM_LINELENGTH, 0, 0);
    
    // Allocate a buffer, make sure there is room for the buffer size
    // even if length is zero...
    char *buffer = new char[length + 1 + sizeof(WORD)];
    
    // Copy the buffer size to the first WORD of the buffer...
    CopyMemory(buffer, &length, sizeof(WORD));
    
    // Copy the string...
    SendMessage(hwndDlgIP, EM_GETLINE, 0, (LPARAM) buffer);
    
    // Add the terminating zero...
    buffer[length] = '\0';
    
    // Show it...
    MessageBox(NULL, buffer, "test", MB_OK);
    
    // Dont forget to delete trash...
    delete buffer;

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up anonytmouse = wicked awesome

    I've been banging my head against a wall for two days..


    anonytmouse.. you are a genius.


    Code:
    void EditBoxFileParser(HWND hwnd, HWND hEdit)
    {
         
         int iCount; 
         WORD iLength;          
         TCHAR **Lines;
         
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         
         if(!iCount)
         
              return;
         
         Lines = new TCHAR*[iCount];
         
         //Populate 2D array - Lines[RowIndex][LineText]
         for(int i=0; i<iCount; i++)
         {     
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, i, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }   
         
         MessageBox(NULL, Lines[3], "test", MB_OK); 
                   
    }

    my only question is.. why does it have to be this hard..?? why wouldn't it work they way Drogin had it set up...??? (which seems to be the intuitively easy way to do it)
    Last edited by The Brain; 10-31-2005 at 05:34 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I completely agree. This dirty old C-functions copying memory and manually adding termanationg zero's and all... I thought that escaping such was the positive side of windows

  15. #15
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    but why
    Code:
    buffer[length] = '\0';
    and not

    Code:
    buffer[length+1] = '\0';
    The length variable does not include the terminating zero...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i make a unique copy of strings?
    By what3v3r in forum C++ Programming
    Replies: 9
    Last Post: 01-12-2006, 09:11 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM