<p>Hmm, no, the terminating NULL should be copied when we pass -1 for original string length.<br>
I think I had to re-read the docs a few times before I convince myself. This part is clear:</p>
<blockquote>
<p>MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character. To null-terminate an output string for this function, the application should pass in -1 or explicitly count the terminating null character for the input string.</p>
</blockquote>
<p>Then the result, less so:</p>
<blockquote>
<p>Returns the number of characters written to the buffer indicated by lpWideCharStr if successful. If the function succeeds and cchWideChar is 0, the return value is the required size, in characters, for the buffer indicated by lpWideCharStr.</p>
</blockquote>
<p>It's the required size for the <strong>buffer</strong>, in characters, not the number of characters for the string (wcslen), so I understand it is including the terminating NULL, if ever we asked for it (with -1 or with strlen()+1).</p>
<p>To be sure:</p>
<pre><code>#include <stdio.h>
#include <windows.h>

int main() {
    char *foo="foo";
    int len = MultiByteToWideChar(CP_UTF8,0,foo,-1,NULL,0);
    printf("len=%d\n",len);
    return 0;
}
</code></pre>
<p>Then</p>
<pre><code>$ i686-w64-mingw32-gcc test.c
$ ./a.exe
$ len=4
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/98fc85ddd56487492a4988ad457dbc2b3fe88397#commitcomment-31813467">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AhLyW7-7NgQPTOr8i6a-ACZWH4z6Gr8lks5u-8fmgaJpZM4Zl6rw">mute the thread</a>.<img src="https://github.com/notifications/beacon/AhLyW_4sGssHI-NeorfQ7XC06l7qLhyeks5u-8fmgaJpZM4Zl6rw.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/OpenSmalltalk/opensmalltalk-vm","title":"OpenSmalltalk/opensmalltalk-vm","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm"}},"updates":{"snippets":[{"icon":"PERSON","message":"@nicolas-cellier-aka-nice on 98fc85d: Hmm, no, the terminating NULL should be copied when we pass -1 for original string length.\r\nI think I had to re-read the docs a few times before I convince myself. This part is clear:\r\n\r\n\u003e MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character. To null-terminate an output string for this function, the application should pass in -1 or explicitly count the terminating null character for the input string.\r\n\r\nThen the result, less so:\r\n\r\n\u003e Returns the number of characters written to the buffer indicated by lpWideCharStr if successful. If the function succeeds and cchWideChar is 0, the return value is the required size, in characters, for the buffer indicated by lpWideCharStr.\r\n\r\nIt's the required size for the **buffer**, in characters, not the number of characters for the string (wcslen), so I understand it is including the terminating NULL, if ever we asked for it (with -1 or with strlen()+1).\r\n\r\nTo be sure:\r\n\r\n    #include \u003cstdio.h\u003e\r\n    #include \u003cwindows.h\u003e\r\n\r\n    int main() {\r\n        char *foo=\"foo\";\r\n        int len = MultiByteToWideChar(CP_UTF8,0,foo,-1,NULL,0);\r\n        printf(\"len=%d\\n\",len);\r\n        return 0;\r\n    }\r\n\r\nThen\r\n\r\n    $ i686-w64-mingw32-gcc test.c\r\n    $ ./a.exe\r\n    $ len=4\r\n"}],"action":{"name":"View Commit","url":"https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/98fc85ddd56487492a4988ad457dbc2b3fe88397#commitcomment-31813467"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/98fc85ddd56487492a4988ad457dbc2b3fe88397#commitcomment-31813467",
"url": "https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/98fc85ddd56487492a4988ad457dbc2b3fe88397#commitcomment-31813467",
"name": "View Commit"
},
"description": "View this Commit on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>