vc2005提示std::copy()不安全。安全版本怎么写?

zxcdewq 2008-11-14 03:43:51
如题
...全文
231 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
DavidHsing 2008-11-16
  • 打赏
  • 举报
回复
StringCchCopyN is a replacement for strncpy. StringCchCopyN copies a given number of characters from a source string. The size, in characters, of the destination buffer is provided to the function to ensure that StringCchCopyN does not write past the end of this buffer.

Syntax

HRESULT StringCchCopyN( LPTSTR pszDest,
size_t cchDest,
LPCTSTR pszSrc,
size_t cchSrc
);
Parameters

pszDest
[out] Pointer to a buffer which receives the copied characters.
cchDest
[in] Size of pszDest, in characters. This value must be large enough to hold the copied characters (the length of pszSrc or the value of cchSrc, whichever is smaller) plus 1 to account for the terminating null character. The maximum number of characters allowed is STRSAFE_MAX_CCH.
pszSrc
[in] Pointer to a buffer containing the source string. This source string must be null-terminated.
cchSrc
[in] The maximum number of characters to copy from pszSrc to pszDest.
Return Value

Note that this function returns an HRESULT as opposed to strncpy, which returns a pointer. It is strongly recommended that you use the SUCCEEDED and FAILED macros to test the return value of this function.

S_OK Source data was present, the characters were copied from pszSrc without truncation, and the resultant destination buffer is null-terminated.
STRSAFE_E_INVALID_PARAMETER The value in cchDest is either larger than STRSAFE_MAX_CCH, or the destination buffer is already full.
STRSAFE_E_INSUFFICIENT_BUFFER The copy operation failed due to insufficient buffer space. The destination buffer contains a truncated, null-terminated version of the intended result. In situations where truncation is acceptable, this may not necessarily be seen as a failure condition.
DavidHsing 2008-11-16
  • 打赏
  • 举报
回复
StringCchCopyN
StringCbCopyN
cdpc0202 2008-11-14
  • 打赏
  • 举报
回复
提示的同时应该会告诉你安全的是哪个。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧