-
July 6th, 2013, 12:13 AM
#1
Smart Pointers / RAII / with Windows API like SendMessage
I'm writing code that requires me to pass a Buffer to Windows via SendMessage. I've been trying to ensure my app is RAII-compliant for good practice.
In the current example I'm trying to write a function that returns the Text of an Edit control as a std::wstring. I can't seem to get a smart pointer (unique_ptr?) to be used.
I've managed to make an ugly solution using try-catch(...), but it feels overkill and "wrong".
1. How would I go about doing this correctly?
2. Also, is there a way I can access std::wstring more directly, and modify it's buffer size, then parse it through the lParam?
My example with the try-catch(...):
Code:
std::wstring Text()
{
int Length = SendMessage(m_hWnd, WM_GETTEXTLENGTH, NULL, NULL);
if (Length <= 0)
return L"";
wchar_t *Buffer = NULL;
std::wstring _Text;
try
{
Buffer = new wchar_t[Length + 1];
SendMessage(m_hWnd, WM_GETTEXT, Length + 1, (LPARAM)Buffer);
_Text = Buffer;
}
catch (...)
{
if (Buffer)
{
delete [] Buffer;
Buffer = NULL;
}
}
if (Buffer)
delete [] Buffer;
return _Text;
};
Thanks.
-
July 6th, 2013, 12:34 AM
#2
Re: Smart Pointers / RAII / with Windows API like SendMessage
Despite searching for a few hours before posting, naturally I find an answer 10 minutes later. Haha.
This is the solution I came across, using a vector.
My solution is now:
Code:
std::wstring Text() const
{
int Length = SendMessage(m_hWnd, WM_GETTEXTLENGTH, NULL, NULL);
if (Length > 0)
{
std::vector<WCHAR> Buffer(Length + 1);
SendMessage(m_hWnd, WM_GETTEXT, Buffer.size(), (LPARAM)&Buffer[0]);
return &Buffer[0];
}
else
return L"";
};
I imagine I can use a vector in the future for similar situations. Any comments still appreciated.
-
July 6th, 2013, 03:44 PM
#3
Re: Smart Pointers / RAII / with Windows API like SendMessage
Originally Posted by FrOzeN89
This is the solution I came across, using a vector.
My solution is now:
Code:
std::wstring Text() const
{
int Length = SendMessage(m_hWnd, WM_GETTEXTLENGTH, NULL, NULL);
if (Length > 0)
{
std::vector<WCHAR> Buffer(Length + 1);
SendMessage(m_hWnd, WM_GETTEXT, Buffer.size(), (LPARAM)&Buffer[0]);
return &Buffer[0];
}
else
return L"";
};
You can use a std::wstring directly; it has a constructor that takes the size of the string and a fill character. See http://www.cplusplus.com/reference/s.../basic_string/ (number 6).
Originally Posted by FrOzeN89
I imagine I can use a vector in the future for similar situations. Any comments still appreciated.
Yes, you can very often use a vector instead of new[].
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|