FIX: ExpandEnvironmentStringsA Returns Wrong Byte Count (234874)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 4.0

This article was previously published under Q234874

SYMPTOMS

ExpandEnvironmentStringsA, which is the ANSI version of ExpandEnvironmentStrings, returns double the number of bytes that are in the string. For example, if the string is 10 characters long, ExpandEnvironmentStringsA returns 20 characters. In fact, ExpandEnvironmentStringsA returns the Unicode byte count, instead of returning the ANSI byte count.

RESOLUTION

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that this article describes. Apply it only to systems that are experiencing this specific problem.

To resolve this problem, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this fix has the file attributes (or later) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.
Date         Time    Size     File name 
--------------------------------------------
15-Mar-2002  17:59   68,415   File1.htc
02-Mar-2002  18:52   32,333   File2.asp
				

Workaround

To work around this issue, determine the real length of the string by using lstrlenA(). Because ExpandEnvironmentStringsA returns a count that includes the trailing NULL, be sure to add 1 (one) to the count returned by lstrlenA().

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

The following code reproduces the behavior:
#include <windows.h>
#include <stdio.h>

#define STR_LEN 40

void main (void)
{
   DWORD ansi_str_len,
         unicode_str_len;

   char psz_ansi_string[STR_LEN];
   WCHAR psz_unicode_string[STR_LEN];


   ansi_str_len = ExpandEnvironmentStringsA ("%systemroot%",
                                             psz_ansi_string, STR_LEN);

   unicode_str_len = ExpandEnvironmentStringsW (L"%systemroot%",
                                                psz_unicode_string,
                                                STR_LEN);

   /*
      ExpandEnvironmentStrings returns number of bytes for the string in
      ANSI (including terminating NULL), or number of characters for the
      string in Unicode (including terminating NULL).

      On Windows NT 4.0, the ANSI version of ExpandEnvironmentStrings 
      returns double the number of bytes that it should.
   */ 
   printf("ExpandEnvironmentStringsA (%s) returned %lu bytes\n",
          psz_ansi_string, ansi_str_len);
   printf("\t It should have returned %lu bytes\n",
          lstrlenA(psz_ansi_string)+1);

   wprintf(L"\nExpandEnvironmentStringsW (%s) returned %lu chars\n",
          psz_unicode_string, unicode_str_len);
   wprintf(L"\t It should have returned %lu chars\n",
           lstrlenW(psz_unicode_string)+1);
}
				

Modification Type:MinorLast Reviewed:10/19/2005
Keywords:kbHotfixServer kbQFE kbAPI kbbug kbfix kbKernBase KB234874