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.