GetEnvironmentVariable function

Retrieves the contents of the specified variable from the environment block of the calling process.


DWORD GetEnvironmentVariable(
  LPCTSTR lpName,
  LPTSTR  lpBuffer,
  DWORD   nSize



The name of the environment variable.


A pointer to a buffer that receives the contents of the specified environment variable as a null-terminated string. An environment variable has a maximum size limit of 32,767 characters, including the null-terminating character.


The size of the buffer pointed to by the lpBuffer parameter, including the null-terminating character, in characters.

Return Value

If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer, not including the terminating null character.

If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters, required to hold the string and its terminating null character and the contents of lpBuffer are undefined.

If the function fails, the return value is zero. If the specified environment variable was not found in the environment block, GetLastError returns ERROR_ENVVAR_NOT_FOUND.


This function can retrieve either a system environment variable or a user environment variable.


For an example, see Changing Environment Variables.


Minimum supported client Windows XP [desktop apps | UWP apps]
Target Platform Windows
Library Kernel32.lib
DLL Kernel32.dll

See Also

Environment Variables