Counting number of characters in a WCHAR string
<p>Lua has a function named <code>utf8.len()</code> which operates on a <code>const char *</code> and does the following according to the docs:</p>

<blockquote>
  <p>Returns the number of UTF-8 characters in string s
  <a href="https://www.lua.org/manual/5.3/manual.html#6.5" rel="nofollow">https://www.lua.org/manual/5.3/manual.html#6.5</a></p>
</blockquote>

<p>I'm working with a customized version of Lua that interfaces with the Win32 API. Whenever I need to pass a UTF-8 string to the Win32 backend of my app, I convert it from UTF-8 to <code>WCHAR</code> using <code>MultiByteToWideChar()</code>.</p>

<p>Now I'm looking for a function that does <em>exactly</em> the same as Lua's <code>utf8.len()</code> function but takes a UTF-16 <code>WCHAR*</code> string instead of a UTF-8 <code>const char*</code>string. Please don't ask me about any Unicode intricacies and terminological discussions. I have already been told that the term <em>character</em> is very ambiguous when talking about Unicode but the Lua documentation uses exactly this term (see above). So what I want is a function that does exactly the same as Lua's <code>utf8.len()</code> but operates on a <code>WCHAR*</code> instead of a <code>const char *</code> ... regardless of what the Lua authors actually mean by <em>character</em>. I just want to have a function that gives me exactly the same count as <code>utf8.len()</code> but operates on a UTF-16 <code>WCHAR*</code> string generated from a UTF-8 string by <code>MultiByteToWideChar()</code>. </p>

<p>I hope the question is now finally sufficiently clear enough... </p>

<p>One last note: I'd like to avoid using external libraries like ICU if possible. Win32 API solutions are preferred.</p>