StringCchPrintf

来源:互联网 发布:快速求斗牛的算法 编辑:程序博客网 时间:2024/05/16 07:45

转自:百度百科  http://baike.baidu.com/view/10100911.htm

目录

简介
声明
参数
注意
实例
相关函数
展开
简介
声明
参数
注意
实例
相关函数
展开

编辑本段简介

StringCchPrintf是sprintf的一个替代品。它接受一个格式字符串和参数列表和返回一个格式化字符串。提供检查功能,确保不越界访问。

编辑本段声明

HRESULT StringCchPrintf( LPTSTR pszDest, size_t cchDest, LPCTSTR pszFormat, ...);
HRESULT StringCchPrintf( LPTSTR pszDest, size_t cchDest, LPCTSTR pszFormat, ...);

编辑本段参数

pszDest

指向目标内存

cchDest

内存区大小。应该足够大,以容纳字符串和结束标记。最大允许的字符数是STRSAFE_MAX_CCH

pszFormat

指向一个缓冲区,其中包含一个printf风格的格式字符串。

其他···

同printf

编辑本段注意

这个函数返回一个HRESULT,而不是像sprintf一样返回存储在其目标缓冲区的字节数。我们强烈建议您使用SUCCEEDED和FAILED宏来测试这个函数的返回值。

结果

S_OKThere表示内存足够用
STRSAFE_E_INVALID_PARAMETERTheb表示cchDest的值是0或大于STRSAFE_MAX_CCH。 STRSAFE_E_INSUFFICIENT_BUFFERThe复制操作失败,原因是没有足够的缓冲空间。目的地缓冲区是结果的一部分。截断是可接受的情况,这可能不一定被视为失败。

编辑本段实例

TCHAR pszDest[30];
size_t cchDest = 30;
LPCTSTR pszFormat = TEXT("%s %d + %d = %d.");
TCHAR* pszTxt = TEXT("The answer is");
HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, 1, 2, 3);
// The resultant string at pszDest is "The answer is 1 + 2 = 3."
TCHAR pszDest[30]; size_t cchDest = 30;LPCTSTR pszFormat = TEXT("%s %d + %d = %d.");TCHAR* pszTxt = TEXT("The answer is");HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, 1, 2, 3);// The resultant string at pszDest is "The answer is 1 + 2 = 3."
Function Information
strsafe.hstrsafe.lib

编辑本段相关函数

StringCbPrintf, StringCchPrintfEx, StringCchVPrintf
扩展阅读:
  • 1

    Remarks

  • 2

    StringCchPrintf provides additional processing for proper buffer handling in your code. Poor buffer handling is implicated in many security issues that involve buffer overruns. StringCchPrintfalways null-terminates a non-zero-length destination buffer.

  • 3

    StringCchPrintf can be used in its generic form, or specifically as StringCchPrintfA (for ANSI strings) or StringCchPrintfW (for Unicode strings). The form to use is determined by your data.

  • 4

    char"string"StringCchPrintfATCHARTEXT("string")StringCchPrintfWCHARL"string"StringCchPrintfW

  • 5

    StringCchPrintf and its ANSI and Unicode variants are replacements for these functions:

  • 6

    sprintfswprintfwsprintfwnsprintf_stprintf_snprintf_snwprintf_sntprintf

  • 7

    Behavior is undefined if the strings pointed to by pszDest, pszFormat, or any argument strings overlap.

  • 8

    Neither pszFormat nor pszDest should be NULL. See StringCchPrintfEx if you require the handling of null string pointer values.

开放分类:
编程 , C++ , API