HOWTO: How To Calculate String Length in Registry (94920)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 3.1
    • the operating system: Microsoft Windows NT 3.5
    • the operating system: Microsoft Windows NT 3.51
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98
    • the operating system: Microsoft Windows Millennium Edition
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows XP

This article was previously published under Q94920

SUMMARY

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.

Modification Type:MinorLast Reviewed:9/27/2004
Keywords:kbhowto kbKernBase kbRegistry KB94920