Smart Pointers / RAII / with Windows API like SendMessage
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Smart Pointers / RAII / with Windows API like SendMessage

Hybrid View

  1. #1
    Join Date
    May 2013
    Posts
    18

    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.

  2. #2
    Join Date
    May 2013
    Posts
    18

    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Smart Pointers / RAII / with Windows API like SendMessage

    Quote Originally Posted by FrOzeN89 View Post
    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).
    Quote Originally Posted by FrOzeN89 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center