Try something like this:
case WM_PASTE:
{
std::wstring cbtext;
if( !OpenClipboard(hwnd) ) // open clipboard
return 0;
// get clipboard data
HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
if( hClipboardData )
{
// Call GlobalLock so that to retrieve a pointer
// to the data associated with the handle returned
// from GetClipboardData.
cbtext = (LPWSTR) GlobalLock(hClipboardData);
// Unlock the global memory.
GlobalUnlock(hClipboardData);
}
// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
if (cbtext.empty())
return 0;
// format the new text with the clipboard data inserted as needed
int len = GetWindowTextLengthW(hwnd);
std::wstring newtext(len, 0);
if (len > 0)
GetWindowTextW(hWnd, &newtext[0], len);
DWORD start, end;
SendMessageW(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
if (end > start)
newtext.replace(start, end-start, cbtext);
else
newtext.insert(start, cbtext);
// parse the new text for validity
// code for parsing text
if( IsTextValid )
SetWindowTextW( hwnd, newtext.c_str() );
return 0;
}