Export (0) Print
Expand All
Expand Minimize
8 out of 15 rated this helpful - Rate this topic

GetEnvironmentVariable function

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

Syntax


DWORD WINAPI GetEnvironmentVariable(
  _In_opt_   LPCTSTR lpName,
  _Out_opt_  LPTSTR lpBuffer,
  _In_       DWORD nSize
);

Parameters

lpName [in, optional]

The name of the environment variable.

lpBuffer [out, optional]

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.

nSize [in]

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.

Remarks

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

Examples

For an example, see Changing Environment Variables.

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

WinBase.h on Windows XP, Windows Server 2003, Windows Vista, Windows 7, Windows Server 2008, and Windows Server 2008 R2 (include Windows.h);
Processenv.h on Windows 8 and Windows Server 2012

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetEnvironmentVariableW (Unicode) and GetEnvironmentVariableA (ANSI)

See also

Environment Variables
GetEnvironmentStrings
SetEnvironmentVariable

 

 

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.