C++ string in a windows function

This is a discussion on C++ string in a windows function within the C++ Programming forums, part of the General Programming Boards category; Hi. For example: If I use GetWindowText, I use it as follows: Code: char WindowCaption [ 1024 ]; GetWindowText ( ...

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    C++ string in a windows function

    Hi.
    For example:

    If I use GetWindowText, I use it as follows:

    Code:
    char WindowCaption [ 1024 ];
    
    GetWindowText ( Window, WindowCaption, 1024 );
    Easy, but let's say I want to do something with the string, to easly work with it, I put it in a C++ string, I prefer to work with C++ strings.

    Code:
    std :: string Caption = WindowCaption;
    Done, only the thing is I need to use two strings.
    I am able to directly use a C++ string in the following code:

    Code:
    MessageBox ( Window, "Message Box Text", Caption.c_str(), MB_OK );
    As you can see I used the c_str() function, but when I try the following, the c_str() function wont help:

    Code:
    std :: string WindowCaption;
    
    GetWindowText ( Window, WindowCaption.c_str(), 1024 );
    It will give me a compile error so I tried a lot of things but they all didn't work.
    I actually have this problem quite long but I never bothered about it, I always just used two strings.
    Only now I really would like to only use one C++ string with GetWindowText, can someone help me how to that?

    Thank you, Yuri.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    I find your signature highly offensive. You may want to change it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    What do you mean? What has that to do with my problem?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Look pal, I'm not even going to go down to your level and explain to you what you know already.

    Your signature is highly offensive. And I'm sure you wouldn't like to see the same bold and neon red text on my signature, but this time talking about netherlands.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Could you stop complaining about my signature, there is nothing wrong with it, you just hate dutch people, really, what is your problem against me?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You cannot pass the string to that function to be filled since string doesn't provide access to modify its internal character data, the c_str() function allows only read access.

    There are different ways to handle the issue. The simplest one is to call GetWindowText as you did with the char WindowCaption[1024] variable, then assign WindowCaption to your string after the call.

    Another one is to use vector<char> WindowCaption(1024), then pass &WindowCaption[0] to the function and assign WindowCaption to your string afterwards. If you know that you will be using 1024, then this is probably overkill, but if the size is dynamic then the vector is a better solution.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    tsk... children.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    'It doesn't provide acces to modify it's internal character data', ok, but why it doesn't?
    To fill a C++ string you are able to use std :: cin >> String; and getline ( cin, String );, in a console application though.

  9. #9
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    tsk... discriminating elderly people.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Those functions are part of the string interface. GetWindowText is not. Those functions are either friends, or they use the same technique that you must use.

    It doesn't provide that access because the original string class designors and/or the standards committee decided it was too unsafe. Part of the point of the string class is to provide safer string funcionality than C style strings, but provide a pointer to internal data makes buffer overruns and other safety issue much more likely. Also, not providing such a pointer might make it easier for library designers to use tricks to optimize the string class.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The best idea is to wrap GetWindowText in a function that first calls GetWindowTextLength (I think it's called), allocates a std::vector of appropriate size, calls GetWindowText to read the text into that buffer and then returns a string that is a copy of the contents of the vector:
    Code:
    template<typename Ch> void getWindowText(HWND hwnd, Ch *buffer, std::size_t len);
    template<> void getWindowText<char>(HWND hwnd, char *buffer, std::size_t len)
    {
      return ::GetWindowTextA(hwnd, buffer, len);
    }
    template<> void getWindowText<wchar_t>(HWND hwnd, wchar_t *buffer, std::size_t len)
    {
      return ::GetWindowTextW(hwnd, buffer, len);
    }
    
    template<typename Ch> std::basic_string<Ch> getWindowText(HWND hwnd)
    {
      std::size_t len = ::GetWindowTextLength(hwnd);
      std::vector<Ch> buffer(len);
      getWindowText(hwnd, buffer, len);
      return std::basic_string<Ch>(buffer.begin(), buffer.end());
    }
    Syntax errors possible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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