Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to pick only the pasted part of a text inside a subclassed editbox while intercepting the WM_PASTE case, in the following way :

C++
case WM_PASTE:
	{
                //i'll later need this two integers to pick only pasted text
		int length = SendMessageW(MainEdit, WM_GETTEXTLENGTH, 0, 0);
		CallWindowProc(DefaultEditProc, hwnd, uMsg, wParam, lParam);
		int length2 = SendMessageW(MainEdit, WM_GETTEXTLENGTH, 0, 0);

		LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, length2 + 1);

		GetWindowTextA(MainEdit, buffer, sizeof(buffer));
                //send message displaying the same wrong output
		//SendMessage(MainEdit, WM_GETTEXT, sizeof(buffer), (LPARAM)buffer);

		MessageBoxA(hwnd, buffer, "nnt", MB_OK);

		GlobalFree(buffer);
    }
    break;


The code above shows in the messagebox only 3 characters out of supposed 16 (or whatever number, for all that matter)

Tried also using a normal buffer :

C++
wchar_t buffer[] = new wchar_t[length2 + 1];
memset(buffer, 0, sizeof(buffer));
//getwindowtext or sendmessage retrieving the same wrong output
GetWindowTextA(MainEdit, buffer, sizeof(buffer));
//GetLastError() always returning 0
delete[] buffer;



If I were to use a normal array buffer the code runs just fine, but since my project involves undefined-lenght pasting processes I can't just place a fixed value.
Posted

1 solution

The error is in this line:
GetWindowTextA(MainEdit, buffer, sizeof(buffer));

You are passing sizeof(buffer) as length which is the size of the pointer which is four with 32-bit builds resulting in copying only three characters and appending a null byte.

You must pass the real size of the buffer instead (here: length2 + 1).

The same applies to the second code including the memset call (which is not necessary).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900