{"id":107450,"date":"2022-11-25T07:00:00","date_gmt":"2022-11-25T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107450"},"modified":"2022-11-24T06:24:31","modified_gmt":"2022-11-24T14:24:31","slug":"20221125-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221125-00\/?p=107450","title":{"rendered":"How do I pass a raw pointer to a Windows Runtime function?"},"content":{"rendered":"<p>A customer was trying to create a Windows Runtime function that accepts a raw pointer, but they got an error:<\/p>\n<pre>namespace MyNamespace\r\n{\r\n    runtimeclass MyClass\r\n    {\r\n        void Transform(Int32 count, Int16* input, Int16* output);\r\n    }\r\n}\r\n<\/pre>\n<p>The compilation files with the error<\/p>\n<pre style=\"white-space: pre-wrap;\">MIDL2025 [msg]syntax error [context]: expecting an identifier near \"*\"\r\n<\/pre>\n<p>What&#8217;s going on?<\/p>\n<p>The Windows Runtime does not support raw pointers at the projection level. Many programming languages don&#8217;t have pointers at all (such as JavaScript and Python), and others restrict their use (C#, Rust). The Windows Runtime tries to be friendly to a large number of languages, and pointers are somewhat out of fashion in modern programming languages because they are so easy to use incorrectly.<\/p>\n<p>In this particular customer&#8217;s case, they wanted to pass a buffer into the method without copying, and to receive the results directly into a caller-provided buffer. Fortunately, these are already expressible with <a title=\"The various patterns for passing C-style arrays across the Windows Runtime ABI boundary\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200205-00\/?p=103398\"> the existing array-passing patterns<\/a>.<\/p>\n<pre>void Transform(Int32[] input, ref Int32[] output);\r\n<\/pre>\n<p>The input buffer is a read-only array, and the output buffer is a write-only array.<\/p>\n<p>For output pointers to single elements, you can use the <code>out<\/code> keyword.<\/p>\n<pre>bool TryGetCount(out Int32 count);\r\n<\/pre>\n<p><b>Bonus reading<\/b>: <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221124-00\/?p=107448\"> How does JavaScript represent output parameters in the Windows Runtime<\/a>?<\/p>\n<p><b>Bonus chatter<\/b>: You may have noticed that I didn&#8217;t mention input pointers to single elements. That&#8217;s because you can just pass those by value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raw pointers don&#8217;t exist in the Windows Runtime, so you&#8217;ll have to re-express it in terms of something that does.<\/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-107450","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Raw pointers don&#8217;t exist in the Windows Runtime, so you&#8217;ll have to re-express it in terms of something that does.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107450","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=107450"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107450\/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=107450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}