How To Calculate String Length in Registry

Last reviewed: January 15, 1997
Article ID: Q94920
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT, versions 3.1, 3.5, 3.51, 4.0
        - Microsoft Windows 95, version 4.0
    

When writing a string to the registry, you must specify the length of the string, including the terminating null character (\0). A common error is to use strlen() to determine the length of the string, but to forget that strlen() returns only the number of characters in the string, not including the null terminator.

Therefore, the length of the string should be calculated as:

   strlen( string ) + 1

Note that a REG_MULTI_SZ string, which contains multiple null-terminated strings, ends with two (2) null characters, which must be factored into the length of the string. For example, a REG_MULTI_SZ string might resemble the following in memory:

   string1\0string2\0string3\0laststring\0\0

When calculating the length of a REG_MULTI_SZ string, add the length of each of the component strings, as above, and add one for the final terminating null.


KBCategory: kbprg kbhowto
KBSubcategory: BseRegistry
Additional reference words: 3.10 3.50 4.00 95


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.