Is there any way to use a STL string as a parameter to GetWindowText?
Printable View
Is there any way to use a STL string as a parameter to GetWindowText?
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. :)
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.