Thread: GetWindowText -> string?

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    GetWindowText -> string?

    Is there any way to use a STL string as a parameter to GetWindowText?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Perhaps, something like:
    Code:
    int txtlen=GetWindowTextLength(hwnd);
    std::string s;  //or use wstring for wchar_t
    s.reserve(txtlen+1);
    GetWindowText(hwnd,const_cast<char*>(s.c_str()), txtlen);
    might do it but I suspect that it may be considered 'evil' to even consider attempting something like this.

    So, I suppose, the realistic answer has to be: no.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well ken that looks pretty evil to me. But one could make function that uses a temporary char buffer to get the message and then copy that buffer into the std::string.

    Code:
    int GetWindowString(HWND hwnd, std::string &s) {
      char buffer[65536];
    
      int txtlen=GetWindowTextLength(hwnd);
      GetWindowText(hwnd, buffer, txtlen);
    
      s = buffer;
      return textlen;
    }
    But I'm sure that XSquared has already done this.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That's basically what I did. I was just wondering if there was a more 'elegant' way to do it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM