{"id":97015,"date":"2017-09-13T07:00:00","date_gmt":"2017-09-13T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97015"},"modified":"2019-03-13T01:16:46","modified_gmt":"2019-03-13T08:16:46","slug":"20170913-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170913-00\/?p=97015","title":{"rendered":"What is the correct way of using the string buffer returned by the WindowsPreallocateStringBuffer function?"},"content":{"rendered":"<p>The most common way of creating an <code>HSTRING<\/code> is to <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20160615-00\/?p=93675\">call <code>Windows&shy;Create&shy;String<\/code><\/a>, but there is also a two-phase creation pattern: First you call <code>Windows&shy;Preallocate&shy;String&shy;Buffer<\/code> to create a buffer for a future string. You then fill the buffer with stringy goodness and then call <code>Windows&shy;Promote&shy;String&shy;Buffer<\/code> to convert it to a real <code>HSTRING<\/code>. (Or you can call <code>Windows&shy;Delete&shy;String&shy;Buffer<\/code> to change your mind and pretend it never happened.) <\/p>\n<p>The rule for managing the buffer returned by <code>Windows&shy;Preallocate&shy;String&shy;Buffer<\/code> is that you are expected to write <i>exactly<\/i> <code>length<\/code> code units into the buffer. No more. No less. The system already put a terminating null after the end of the buffer; your job is to fill the buffer with the string contents. <\/p>\n<p>For example, if you want to use two-phase creation to create the string <code>hello<\/code>, you would call <code>Windows&shy;Preallocate&shy;String&shy;Buffer<\/code> and pass <code>length = 5<\/code>. Into the resulting buffer, you write the characters <code>h<\/code>, <code>e<\/code>, <code>l<\/code>, <code>l<\/code>, and <code>o<\/code>, and that&#8217;s all. The system already stored the terminating null. <\/p>\n<p>This particular formulation of the rules is important in the case that <code>length = 0<\/code>.&sup1; Since the representation of an <code>HSTRING<\/code> of length zero is the null pointer, there is no actual buffer. What happens is that the system uses a single preallocated buffer (consisting of just a null terminator) to represent the buffer for all zero-length strings. If you call <code>Windows&shy;Preallocate&shy;String&shy;Buffer<\/code>, you get a pointer to that preallocated buffer.&sup2; Since you passed a length of zero, you are expected to write zero characters to the buffer; in other words, you are expected to do nothing at all with the buffer. <\/p>\n<p>And of course since <code>HSTRING<\/code>s are immutable, your permission to modify the buffer ends once you promote the buffer to a string. Once it&#8217;s been promoted to a string, the entire buffer becomes read-only. <\/p>\n<p>&sup1; Another way of interpreting this corner case is to say &#8220;Don&#8217;t bother calling <code>Windows&shy;Preallocate&shy;String&shy;Buffer<\/code> with a string of length zero. Otherwise, go ahead and call it, and you can write that null terminator if you like.&#8221; <\/p>\n<p>&sup2; Arguably, to accommodate the possibiltiy of somebody writing that null terminator, it should return a preallocated <i>writable<\/i> buffer just large enough to hold that null terminator. It could be the high 16 bits of the <code>length<\/code> field itself! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write the characters you allocated, but only those characters.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-97015","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Write the characters you allocated, but only those characters.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=97015"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97015\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=97015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}