Unicode strlen equivalent in MFC
Consider the following simple code:
GetDlgItemText(IDC_EName,LPTSTR(cName),11);
k=strlen(cName);
I want to 开发者_StackOverflow社区get the length of String that user puts at edit box but I have the wrong result K=1 (always) because it's unicode string and it get the first character and the second is null and I donot know how to fix it. Any comment is welcome. Regards,
The length of the string, or more accurately the amount of characters (not bytes) copied to the output buffer is returned by your call to GetDlgItemText()
.
You can also check the length of the string directly. Since you use ANSI/Unicode compatible macros such as LPTSTR
, you should use a ANSI/Unicode compatible strlen function: _tcslen(cName)
_tcslen()
resolves to strlen()
when compiling to ANSI/MBCS and to wcslen()
when compiling to Unicode.
Use wcslen()
http://msdn.microsoft.com/en-us/library/78zh94ax%28v=vs.80%29.aspx
Of course you could just do:
k = GetDlgItemText(IDC_EName,LPTSTR(cName),11);
since the return value specifies the number of characters copied to the buffer. http://msdn.microsoft.com/en-us/library/ms645489%28v=vs.85%29.aspx
精彩评论