Click to See Complete Forum and Search --> : EM_GETLINE with vector


MasterDucky
November 5th, 2009, 12:23 PM
Hello!

Im trying to use EM_GETLINE with vector<char> instead of a dynamic char array.

Its working with some minor bugs:
- if i type only one letter i get one garbage character
- there are some garbage at the end of the lines sometimes

Its maybe the vector functions that i dont use right.

Any suggestions highly appreciated.


case GetText:
{
int iCount, i;
WORD iLength, iOffset;
string buffer;
vector<char> vLines;

//Get Number of Lines in Edit Field
iCount = SendMessage(hWin_2, EM_GETLINECOUNT, 0, 0);

if(!iCount) return 0;

for(i=0; i<iCount; i++)
{
iOffset = SendMessage(hWin_2, EM_LINEINDEX, i, 0);
iLength = (WORD)SendMessage(hWin_2, EM_LINELENGTH, iOffset, 0);
vLines.clear();
vLines.resize( iLength + sizeof(WORD)+1 );

CopyMemory(&vLines[0], &iLength, sizeof(WORD)+1);
SendMessage(hWin_2, EM_GETLINE, i, (LPARAM)&vLines[0]);

vLines.push_back( '\0' ); //do i need this?

SendMessage(hWin_1, LB_INSERTSTRING, -1, (LPARAM)&vLines[0]);

}

Codeplug
November 5th, 2009, 01:07 PM
>> CopyMemory(&vLines[0], &iLength, sizeof(WORD)+1);
You are copying one to many bytes. Also, you'll want to copy in the same value that was passed to resize() - since that's the size of the buffer.

>> vLines.push_back( '\0' ); //do i need this?
Not like that. EM_GETLINE returns the number of characters copied. Just use "vLines[num_copied] = 0". Your buffer will then be a null-terminated C-string.

gg

MasterDucky
November 6th, 2009, 12:35 AM
Perfect! Thank you very much Codeplug!