Thread: GetWindowText() help

  1. #1
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53

    GetWindowText() help

    Code:
    PTSTR buffer;
    int length;
    
    ...
    
    length = GetWindowTextLength(hwndEdit);
    
    GetWindowText(hwndEdit, buffer, length +1);
    Shouldn't buffer now have the text from the edit box? It doesn't.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    No, you have to allocate memory to hold the string, eg. 1
    Code:
    TCHAR buffer[256];
    GetWindowText(hwnd,buffer,255);
    which runs the risk of truncation if the text is longer. Or, eg. 2
    Code:
    TCHAR *buffer;
    int length;
    length=GetWindowTextLength(hwnd);
    buffer=new TCHAR[length+1]; /*or use malloc in C */
    GetWindowText(hwnd,buffer,length);
    /*and when you're done, free up the memory*/
    delete[] buffer; /*or use free if memory allocated with malloc */
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    It works

    Thanks again to everyone that helped me with this, I couldn't have done it with you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hung up on GetWindowText
    By Viper187 in forum Windows Programming
    Replies: 18
    Last Post: 09-06-2008, 08:30 AM
  2. GetWindowText() not working with Vista???
    By Abda92 in forum Windows Programming
    Replies: 10
    Last Post: 04-07-2008, 01:29 PM
  3. GetwindowText problem
    By spanker in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2008, 10:25 AM
  4. GetWindowText doesn't return the text
    By Rune Hunter in forum Windows Programming
    Replies: 16
    Last Post: 09-23-2005, 04:49 PM
  5. Using GetWindowText
    By ColdFire in forum Windows Programming
    Replies: 9
    Last Post: 05-26-2002, 10:24 PM