使用 StringCbPrintf 格式化缓冲区
Formatting a buffer with StringCbPrintf
>我在如何使用StringCbPrintf格式化缓冲区方面遇到困难,这里应该是一个HTTP请求:
char getExternalIpRequest[1200];
ZeroMemory(getExternalIpRequest, 1200);
StringCbPrintf(getExternalIpRequest, 1200,
"<?xml version="1.0"?>"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">"
"<SOAP-ENV:Body>"
"<m:AddPortMapping xmlns:m="urn:schemas-upnp-org:service:WANIPConnection:1">"
"<NewRemoteHost xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string">"
""
"</NewRemoteHost>"
"<NewExternalPort xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="ui2">"
"%s"
"</NewExternalPort>"
"<NewProtocol xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string">"
"%s"
"</NewProtocol>"
"<NewInternalPort xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="ui2">"
"%s"
"</NewInternalPort>"
"<NewInternalClient xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string">"
"%s"
"</NewInternalClient>"
"<NewEnabled xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="boolean">"
"1"
"</NewEnabled>"
"<NewPortMappingDescription xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string">"
"%s"
"</NewPortMappingDescription>"
"<NewLeaseDuration xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="ui4">"
"0"
"</NewLeaseDuration>"
"</m:AddPortMapping>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>rnrn", externalPort, protocol, internalPort, internalp, entryDescription);
// externalPort, protocol, internalPort, internalp, entryDescription are **char*** type.
char getExternalIpRequestHeader[1500];
ZeroMemory(getExternalIpRequestHeader, 1500);
StringCbPrintf(getExternalIpRequestHeader, 1500,
"POST /UD/?3 HTTP/1.1rn"
"Content-Type: text/xml; charset="utf-8"rn"
"SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"rn"
"User-Agent: Mozilla/4.0 (compatible; UPnP/1.0; Windows 9x)rn"
"Host: %srn"
"Content-Length: %srn"
"Connection: Keep-Alivern"
"Cache-Control: no-cachern"
"Pragma: no-cachernrn", upnpDeviceIp, strlen(getExternalIpRequest));
但是当我在控制台中回显它时,我收到访问冲突消息。
更新我注意到如果我评论StringCbPrintf(getExternalIpRequest)或StringCbPrintf(getExternalIpRequestHeader)之一,就不会崩溃。
格式化getExternalIpRequestHeader
时,Content-Length
标头使用 %s
,它需要一个char*
值,但你为它提供的返回值是 strlen()
,这是一个int
,所以你需要改用%d
。
相关文章:
- C++字符*缓冲区的大小
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- ostream过载时的缓冲区冲洗
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- C++格式化输出问题
- Xaudio2在更改缓冲区或循环时弹出声音
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 多线程双缓冲区
- Android P-9.0.0_r53 Logcat主缓冲区超出定义大小
- 套接字读取后,我在缓冲区中看到意外输入
- 格式化浮点值:返回默认值
- std::带有自定义缓冲区的 iostream 不允许我写入
- 如何在C++中对静态缓冲区执行字符串格式化?
- 需要从格式化字符串移动到二进制缓冲区
- 使用 StringCbPrintf 格式化缓冲区
- 其中是printf的格式化缓冲区
- 是否可以使用Boost.使用预分配的缓冲区格式化