C++ Windows: Saving the line of an edit control into a vector of TCHAR

The following code works fine:

int iLength = SendMessage(hWndEdit, EM_LINELENGTH, 0, 0);
if(iLength > 0) {
    TCHAR* szBuffer = new TCHAR[iLength+1];
    iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)szBuffer);
    szBuffer[iLength] = TEXT('\0');
    MessageBox(hWnd, szBuffer, TEXT("Edit content"), MB_OK);
    delete[] szBuffer;

Now I want to do the same thing using std::vector<TCHAR>:

int iLength = SendMessage(hWndEdit, EM_LINELENGTH, 0, 0);
if(iLength > 0) {
    std::vector<TCHAR> data(iLength+1, TEXT('\0'));
    iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)&data[0]);
    MessageBox(hWnd, &data[0], TEXT("Edit content"), MB_OK);

It compiles, but at iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)&data[0]); the variable iLength will always be set to 0, and, accordingly, nothing will have been written into my vector.

What is the problem here? Thanks in advance.


From the documentation:

A pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer.

Need Your Help

max(id) and limit 10, but use them in different places

php mysql arrays limit fetch

I have two tables, posts and sections. I want to get the last 10 posts WHERE section = 1,

(Java) Using GridLayout (possibly GridBagLayout) on a 2D array of JPanels

java swing jpanel grid-layout gridbaglayout

I am trying to make a program that allows the user to change the colour of JPanels through mouse interaction. I have the array of JPanels all up and running (5x5) BUT they expand to fit the entire ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.