0

目前,Microsoft 没有为较新的 c++ 字符类型提供等效的StringCbLengthA和:和. 所以,我决定为新的字符类型复制当前的实现。对于,我添加了这个函数:StringCbLengthWchar16_tchar32_tchar16_t

typedef  const char16_t* STRSAFE_PCNZUCH;

_Must_inspect_result_ STRSAFEAPI StringCbLengthU(
    _In_reads_or_z_(cbMax / sizeof(char16_t)) STRSAFE_PCNZUCH psz,
    _In_ _In_range_(1, STRSAFE_MAX_CCH * sizeof(char16_t)) size_t cbMax,
    _Out_opt_ _Deref_out_range_(< , cbMax - 1) size_t* pcbLength)
{
    HRESULT hr;
    size_t cchMax = cbMax / sizeof(char16_t);
    size_t cchLength = 0;

    if ((psz == NULL) || (cchMax > STRSAFE_MAX_CCH))
    {
        hr = STRSAFE_E_INVALID_PARAMETER;
    }
    else
    {
        hr = StringLengthWorkerU(psz, cchMax, &cchLength);
    }

    if (pcbLength)
    {
        if (SUCCEEDED(hr))
        {
            // safe to multiply cchLength * sizeof(char16_t) since cchLength < STRSAFE_MAX_CCH and sizeof(char16_t) is 2
            *pcbLength = cchLength * sizeof(char16_t);
        }
        else
        {
            *pcbLength = 0;
        }
    }

    return hr;
}

但是,这会生成C28285类型的警告。无论如何,要修复此代码以避免此警告?

4

0 回答 0