CEdit::GetLine() windows 7

女生的网名这么多〃 提交于 2019-12-10 18:19:48

问题


I have the following segment of code where m_edit is a CEdit control:

TCHAR lpsz[MAX_PATH+1];

// get the edit box text
m_edit.GetLine(0,lpsz, MAX_PATH); 

This works perfectly on computers running Windows XP and earlier. I have not tested this in Vista, but on Windows 7, lpsz gets junk unicode characters inserted into it (as well as the actual text sometimes). Any idea as to what is going on here?


回答1:


Since you're using MFC, why aren't you taking advantage of its CString class? That's one of the reasons many programmers were drawn to MFC, because it makes working with strings so much easier.

For example, you could simply write:

int len = m_edit.LineLength(m_edit.LineIndex(0));
CString path;
LPTSTR p = path.GetBuffer(len);
m_edit.GetLine(0, p, len);
path.ReleaseBuffer();

(The above code is tested to work fine on Windows 7.)

Note that the copied line does not contain a null-termination character (see the "Remarks" section in the documentation). That could explain the nonsense characters you're seeing in later versions of Windows.




回答2:


It's not null terminated. You need to do this:

int count = m_edit.GetLine(0, lpsz, MAX_PATH);
lpsz[count] = 0;


来源:https://stackoverflow.com/questions/5637153/ceditgetline-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!