Is there any way to use a STL string as a parameter to GetWindowText?
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
Perhaps, something like:
might do it but I suspect that it may be considered 'evil' to even consider attempting something like this.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);
So, I suppose, the realistic answer has to be: no.
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
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.
But I'm sure that XSquared has already done this.Code:int GetWindowString(HWND hwnd, std::string &s) { char buffer[65536]; int txtlen=GetWindowTextLength(hwnd); GetWindowText(hwnd, buffer, txtlen); s = buffer; return textlen; }
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