Skip to content

Commit 1c8f864

Browse files
oldnewthingKarl-Bridge-Microsoft
authored andcommitted
Merged PR 18551: WindowsPreallocateStringBuffer: Clarify status of null terminator
Starting in RS4, you can also write to the pre-allocated null terminator. Also fix the sample so it (1) includes the correct header file, and (2) writes to the string buffer as required by the API.
2 parents 1350380 + 496efbc commit 1c8f864

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

sdk-api-src/content/winstring/nf-winstring-windowspreallocatestringbuffer.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Failed to allocate the [**HSTRING**](/windows/win32/winrt/hstring) buffer.
131131
## -remarks
132132

133133
Use the <b>WindowsPreallocateStringBuffer</b> function to create a mutable character buffer that you can manipulate prior to committing it to an immutable [**HSTRING**](/windows/win32/winrt/hstring). When you have finished populating the <i>mutableBuffer</i> with your string, call the <a href="/windows/desktop/api/winstring/nf-winstring-windowspromotestringbuffer">WindowsPromoteStringBuffer</a> function with the <i>bufferHandle</i> parameter to create the <b>HSTRING</b>. You must write exactly <i>length</i> characters into the buffer.
134+
<b>Windows 10 Version 1803, Windows Server Version 1803, and later</b>: You are permitted to write a null terminator after <i>length</i> characters.
134135

135136
Call the <a href="/windows/desktop/api/winstring/nf-winstring-windowsdeletestringbuffer">WindowsDeleteStringBuffer</a> function to discard the mutable buffer prior to promotion. If the buffer has already been promoted by a call to <a href="/windows/desktop/api/winstring/nf-winstring-windowspromotestringbuffer">WindowsPromoteStringBuffer</a>, call the <a href="/windows/desktop/api/winstring/nf-winstring-windowsdeletestring">WindowsDeleteString</a> function to discard the string. If the <b>WindowsPromoteStringBuffer</b> call fails, you can call the <b>WindowsDeleteStringBuffer</b> function to discard the mutable buffer.
136137

@@ -139,7 +140,7 @@ Call the <a href="/windows/desktop/api/winstring/nf-winstring-windowsdeletestrin
139140
The following code example demonstrates how to use the <b>WindowsPreallocateStringBuffer</b> function.
140141

141142
```cpp
142-
#include <WinrtString.h>
143+
#include <winstring.h>
143144

144145
int main()
145146
{
@@ -151,6 +152,7 @@ int main()
151152

152153
if (SUCCEEDED(hr))
153154
{
155+
CopyMemory(strBuffer, L"1234567890", 10 * sizeof(wchar_t));
154156
hr = WindowsPromoteStringBuffer(hStringBuffer, &hString);
155157
}
156158

0 commit comments

Comments
 (0)