📄 strsafe.h
字号:
return value of this function
--*/
STRSAFEAPI StringCchCopyNExA(char* pszDest, size_t cchDest, const char* pszSrc, size_t cchSrc, char** ppszDestEnd, size_t* pcchRemaining, unsigned long dwFlags);
STRSAFEAPI StringCchCopyNExW(wchar_t* pszDest, size_t cchDest, const wchar_t* pszSrc, size_t cchSrc, wchar_t** ppszDestEnd, size_t* pcchRemaining, unsigned long dwFlags);
#ifdef UNICODE
#define StringCchCopyNEx StringCchCopyNExW
#else
#define StringCchCopyNEx StringCchCopyNExA
#endif // !UNICODE
#ifdef STRSAFE_INLINE
STRSAFEAPI StringCchCopyNExA(char* pszDest, size_t cchDest, const char* pszSrc, size_t cchSrc, char** ppszDestEnd, size_t* pcchRemaining, unsigned long dwFlags)
{
HRESULT hr;
if ((cchDest > STRSAFE_MAX_CCH) ||
(cchSrc > STRSAFE_MAX_CCH))
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
size_t cbDest;
// safe to multiply cchDest * sizeof(char) since cchDest < STRSAFE_MAX_CCH and sizeof(char) is 1
cbDest = cchDest * sizeof(char);
hr = StringCopyNExWorkerA(pszDest, cchDest, cbDest, pszSrc, cchSrc, ppszDestEnd, pcchRemaining, dwFlags);
}
return hr;
}
STRSAFEAPI StringCchCopyNExW(wchar_t* pszDest, size_t cchDest, const wchar_t* pszSrc, size_t cchSrc, wchar_t** ppszDestEnd, size_t* pcchRemaining, unsigned long dwFlags)
{
HRESULT hr;
if ((cchDest > STRSAFE_MAX_CCH) ||
(cchSrc > STRSAFE_MAX_CCH))
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
size_t cbDest;
// safe to multiply cchDest * sizeof(wchar_t) since cchDest < STRSAFE_MAX_CCH and sizeof(wchar_t) is 2
cbDest = cchDest * sizeof(wchar_t);
hr = StringCopyNExWorkerW(pszDest, cchDest, cbDest, pszSrc, cchSrc, ppszDestEnd, pcchRemaining, dwFlags);
}
return hr;
}
#endif // STRSAFE_INLINE
#endif // !STRSAFE_NO_CCH_FUNCTIONS
#ifndef STRSAFE_NO_CB_FUNCTIONS
/*++
STDAPI
StringCbCopyNEx(
OUT LPTSTR pszDest OPTIONAL,
IN size_t cbDest,
IN LPCTSTR pszSrc OPTIONAL,
IN size_t cbSrc,
OUT LPTSTR* ppszDestEnd OPTIONAL,
OUT size_t* pcbRemaining OPTIONAL,
IN DWORD dwFlags
);
Routine Description:
This routine is a safer version of the C built-in function 'strncpy' with
some additional parameters. In addition to functionality provided by
StringCbCopyN, this routine also returns a pointer to the end of the
destination string and the number of bytes left in the destination string
including the null terminator. The flags parameter allows additional controls.
This routine is meant as a replacement for strncpy, but it does behave
differently. This function will not pad the destination buffer with extra
null termination characters if cbSrc is greater than the size of pszSrc.
Arguments:
pszDest - destination string
cbDest - size of destination buffer in bytes.
length must be ((_tcslen(pszSrc) + 1) * sizeof(TCHAR)) to
hold all of the source including the null terminator
pszSrc - source string
cbSrc - maximum number of bytes to copy from source string
ppszDestEnd - if ppszDestEnd is non-null, the function will return a
pointer to the end of the destination string. If the
function copied any data, the result will point to the
null termination character
pcbRemaining - pcbRemaining is non-null,the function will return the
number of bytes left in the destination string,
including the null terminator
dwFlags - controls some details of the string copy:
STRSAFE_FILL_BEHIND_NULL
if the function succeeds, the low byte of dwFlags will be
used to fill the uninitialize part of destination buffer
behind the null terminator
STRSAFE_IGNORE_NULLS
treat NULL string pointers like empty strings (TEXT("")).
this flag is useful for emulating functions like lstrcpy
STRSAFE_FILL_ON_FAILURE
if the function fails, the low byte of dwFlags will be
used to fill all of the destination buffer, and it will
be null terminated. This will overwrite any truncated
string returned when the failure is
STRSAFE_E_INSUFFICIENT_BUFFER
STRSAFE_NO_TRUNCATION /
STRSAFE_NULL_ON_FAILURE
if the function fails, the destination buffer will be set
to the empty string. This will overwrite any truncated string
returned when the failure is STRSAFE_E_INSUFFICIENT_BUFFER.
Notes:
Behavior is undefined if source and destination strings overlap.
pszDest and pszSrc should not be NULL unless the STRSAFE_IGNORE_NULLS flag
is specified. If STRSAFE_IGNORE_NULLS is passed, both pszDest and pszSrc
may be NULL. An error may still be returned even though NULLS are ignored
due to insufficient space.
Return Value:
S_OK - if there was source data and it was all copied and the
resultant dest string was null terminated
failure - you can use the macro HRESULT_CODE() to get a win32
error code for all hresult failure cases
STRSAFE_E_INSUFFICIENT_BUFFER /
HRESULT_CODE(hr) == ERROR_INSUFFICIENT_BUFFER
- this return value is an indication that the copy
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result and is
null terminated. This is useful for situations where
truncation is ok.
It is strongly recommended to use the SUCCEEDED() / FAILED() macros to test the
return value of this function
--*/
STRSAFEAPI StringCbCopyNExA(char* pszDest, size_t cbDest, const char* pszSrc, size_t cbSrc, char** ppszDestEnd, size_t* pcbRemaining, unsigned long dwFlags);
STRSAFEAPI StringCbCopyNExW(wchar_t* pszDest, size_t cbDest, const wchar_t* pszSrc, size_t cbSrc, wchar_t** ppszDestEnd, size_t* pcbRemaining, unsigned long dwFlags);
#ifdef UNICODE
#define StringCbCopyNEx StringCbCopyNExW
#else
#define StringCbCopyNEx StringCbCopyNExA
#endif // !UNICODE
#ifdef STRSAFE_INLINE
STRSAFEAPI StringCbCopyNExA(char* pszDest, size_t cbDest, const char* pszSrc, size_t cbSrc, char** ppszDestEnd, size_t* pcbRemaining, unsigned long dwFlags)
{
HRESULT hr;
size_t cchDest;
size_t cchSrc;
size_t cchRemaining = 0;
cchDest = cbDest / sizeof(char);
cchSrc = cbSrc / sizeof(char);
if ((cchDest > STRSAFE_MAX_CCH) ||
(cchSrc > STRSAFE_MAX_CCH))
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
hr = StringCopyNExWorkerA(pszDest, cchDest, cbDest, pszSrc, cchSrc, ppszDestEnd, &cchRemaining, dwFlags);
}
if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER))
{
if (pcbRemaining)
{
// safe to multiply cchRemaining * sizeof(char) since cchRemaining < STRSAFE_MAX_CCH and sizeof(char) is 1
*pcbRemaining = (cchRemaining * sizeof(char)) + (cbDest % sizeof(char));
}
}
return hr;
}
STRSAFEAPI StringCbCopyNExW(wchar_t* pszDest, size_t cbDest, const wchar_t* pszSrc, size_t cbSrc, wchar_t** ppszDestEnd, size_t* pcbRemaining, unsigned long dwFlags)
{
HRESULT hr;
size_t cchDest;
size_t cchSrc;
size_t cchRemaining = 0;
cchDest = cbDest / sizeof(wchar_t);
cchSrc = cbSrc / sizeof(wchar_t);
if ((cchDest > STRSAFE_MAX_CCH) ||
(cchSrc > STRSAFE_MAX_CCH))
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
hr = StringCopyNExWorkerW(pszDest, cchDest, cbDest, pszSrc, cchSrc, ppszDestEnd, &cchRemaining, dwFlags);
}
if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER))
{
if (pcbRemaining)
{
// safe to multiply cchRemaining * sizeof(wchar_t) since cchRemaining < STRSAFE_MAX_CCH and sizeof(wchar_t) is 2
*pcbRemaining = (cchRemaining * sizeof(wchar_t)) + (cbDest % sizeof(wchar_t));
}
}
return hr;
}
#endif // STRSAFE_INLINE
#endif // !STRSAFE_NO_CB_FUNCTIONS
#ifndef STRSAFE_NO_CCH_FUNCTIONS
/*++
STDAPI
StringCchCat(
IN OUT LPTSTR pszDest,
IN size_t cchDest,
IN LPCTSTR pszSrc
);
Routine Description:
This routine is a safer version of the C built-in function 'strcat'.
The size of the destination buffer (in characters) is a parameter and this
function will not write past the end of this buffer and it will ALWAYS
null terminate the destination buffer (unless it is zero length).
This function returns a hresult, and not a pointer. It returns
S_OK if the string was concatenated without truncation and null terminated,
otherwise it will return a failure code. In failure cases as much of pszSrc
will be appended to pszDest as possible, and pszDest will be null
terminated.
Arguments:
pszDest - destination string which must be null terminated
cchDest - size of destination buffer in characters.
length must be = (_tcslen(pszDest) + _tcslen(pszSrc) + 1)
to hold all of the combine string plus the null
terminator
pszSrc - source string which must be null terminated
Notes:
Behavior is undefined if source and destination strings overlap.
pszDest and pszSrc should not be NULL. See StringCchCatEx if you require
the handling of NULL values.
Return Value:
S_OK - if there was source data and it was all concatenated and
the resultant dest string was null terminated
failure - you can use the macro HRESULT_CODE() to get a win32
error code for all hresult failure cases
STRSAFE_E_INSUFFICIENT_BUFFER /
HRESULT_CODE(hr) == ERROR_INSUFFICIENT_BUFFER
- this return value is an indication that the operation
failed due to insufficient space. When this error occurs,
the destination buffer is modified to contain a truncated
version of the ideal result and is null terminated. This
is useful for situations where truncation is ok.
It is strongly recommended to use the SUCCEEDED() / FAILED() macros to test the
return value of this function
--*/
STRSAFEAPI StringCchCatA(char* pszDest, size_t cchDest, const char* pszSrc);
STRSAFEAPI StringCchCatW(wchar_t* pszDest, size_t cchDest, const wchar_t* pszSrc);
#ifdef UNICODE
#define StringCchCat StringCchCatW
#else
#define StringCchCat StringCchCatA
#endif // !UNICODE
#ifdef STRSAFE_INLINE
STRSAFEAPI StringCchCatA(char* pszDest, size_t cchDest, const char* pszSrc)
{
HRESULT hr;
if (cchDest > STRSAFE_MAX_CCH)
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
hr = StringCatWorkerA(pszDest, cchDest, pszSrc);
}
return hr;
}
STRSAFEAPI StringCchCatW(wchar_t* pszDest, size_t cchDest, const wchar_t* pszSrc)
{
HRESULT hr;
if (cchDest > STRSAFE_MAX_CCH)
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
hr = StringCatWorkerW(pszDest, cchDest, pszSrc);
}
return hr;
}
#endif // STRSAFE_INLINE
#endif // !STRSAFE_NO_CCH_FUNCTIONS
#ifndef STRSAFE_NO_CB_FUNCTIONS
/*++
STDAPI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -