Can anyone tell me if this is the correct usage of the new string functions?
//(lpalp is basically just an object that is a struct that contains a
_TCHAR* and its length)
size_t destlen = (size_t)lpalp->maxbufferlen;
_TCHAR username[TEXTLIMIT + 1], password[TEXTLIMIT + 1];
UINT uLenUsername = GetDlgItemText(hWnd, IDC_USERNAME, username, TEXTLIMIT),
uLenPassword = GetDlgItemText(hWnd, IDC_PASSWORD, password, TEXTLIMIT);
#if _MSC_VER >= 1400
_tcscpy_s(lpalp->buffer, destlen, _T("User ID="));
_tcsncat_s(lpalp->buffer, destlen, username, (size_t)uLenUsername);
_tcscat_s(lpalp->buffer, destlen, _T(";Password="));
_tcsncat_s(lpalp->buffer, destlen, password, uLenPassword);
#else
long charsleft = lpalp->maxbufferlen - 1;
_tcsncpy(lpalp->buffer, U_PREFIX, charsleft); charsleft -=
_tcslen(U_PREFIX);
_tcsncat(lpalp->buffer, username, charsleft); charsleft -=
uLenUsername;
_tcsncat(lpalp->buffer, P_PREFIX, charsleft); charsleft -=
_tcslen(P_PREFIX);
_tcsncat(lpalp->buffer, password, charsleft);
#endif
Have I got it right by this?
Steve Friedl [MVP] - 28 Nov 2004 17:27 GMT
> size_t destlen = (size_t)lpalp->maxbufferlen;
> _TCHAR username[TEXTLIMIT + 1], password[TEXTLIMIT + 1];
[quoted text clipped - 16 lines]
> _tcsncat(lpalp->buffer, password, charsleft);
> #endif
This is really the hard way, and though it looks mostly right on a technical
basis, this is so tedious that it takes the patience of St. Francis to do
this everywhere. Why not use _sntprintf, which does the formatting for you
(though it doesn't automatically add the NUL byte, dammit):
_sntprintf(lpalp->buffer, lpalp->maxbufferlen,
_T("User ID=%s;Password=%s"),
username,
password);
lpalp->buffer[ lpalp->maxbufferlen-1 ] = '\0';
This way your purpose is very clear, it's safe, and it's portable.
~~ Steve
Bonj - 28 Nov 2004 18:24 GMT
oh god yes, that's much better
Thanks Steve!
>> size_t destlen = (size_t)lpalp->maxbufferlen;
>> _TCHAR username[TEXTLIMIT + 1], password[TEXTLIMIT + 1];
[quoted text clipped - 33 lines]
>
> ~~ Steve