Skip to main content
ExpandEnvironmentStrings function

Expands environment-variable strings and replaces them with the values defined for the current user.

To specify the environment block for a particular user or the system, use the ExpandEnvironmentStringsForUser function.


DWORD WINAPI ExpandEnvironmentStrings(
  _In_      LPCTSTR lpSrc,
  _Out_opt_ LPTSTR  lpDst,
  _In_      DWORD   nSize


lpSrc [in]

A buffer that contains one or more environment-variable strings in the form: %variableName%. For each such reference, the %variableName% portion is replaced with the current value of that environment variable.

Case is ignored when looking up the environment-variable name. If the name is not found, the %variableName% portion is left unexpanded.

Note that this function does not support all the features that Cmd.exe supports. For example, it does not support %variableName:str1=str2% or %variableName:~offset,length%.

lpDst [out, optional]

A pointer to a buffer that receives the result of expanding the environment variable strings in the lpSrc buffer. Note that this buffer cannot be the same as the lpSrc buffer.

nSize [in]

The maximum number of characters that can be stored in the buffer pointed to by the lpDst parameter. When using ANSI strings, the buffer size should be the string length, plus terminating null character, plus one. When using Unicode strings, the buffer size should be the string length plus the terminating null character.

Return value

If the function succeeds, the return value is the number of TCHARs stored in the destination buffer, including the terminating null character. If the destination buffer is too small to hold the expanded string, the return value is the required buffer size, in characters.

If the function fails, the return value is zero. To get extended error information, call GetLastError.


The size of the lpSrc and lpDst buffers is limited to 32K.

To replace folder names in a fully qualified path with their associated environment-variable strings, use the PathUnExpandEnvStrings function.

To retrieve the list of environment variables for a process, use the GetEnvironmentStrings function.


For an example, see Getting System Information.


Minimum supported client

Windows 2000 Professional [desktop apps | UWP apps]

Minimum supported server

Windows 2000 Server [desktop apps | UWP apps]


Winbase.h (include Windows.h)





Unicode and ANSI names

ExpandEnvironmentStringsW (Unicode) and ExpandEnvironmentStringsA (ANSI)

See also

Environment Variables
System Information Functions