5 out of 10 rated this helpful - Rate this topic

GetEnvironmentVariable function

Applies to: desktop apps only

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

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetEnvironmentVariableW (Unicode) and GetEnvironmentVariableA (ANSI)

See also

Environment Variables
GetEnvironmentStrings
SetEnvironmentVariable

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
GetEnvironmentVariable Doesn't work with UTF8 Data
While it may seem to be obvious, let point out that reading & writing UTF-8 data from the environment using GetEnvironmentVariable() and SetEnvironmentVariable() doesn't work.  In the case of chinese characters it will either drop the last byte (if there is an odd number of bytes in the string) or remove it all together.
Alternatives to this unmanaged API

As an alternative to this unmanaged API, you should look at System.Environment and the GetEnvironmentVariable method. For more information on the class, see http://msdn.microsoft.com/en-us/library/system.environment.aspx. For more info on the GetEnvironmentVariable method, see: http://msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable.aspx

C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetEnvironmentVariable(ByVal lpName As String, ByVal lpValue As StringBuilder, ByVal size As Integer) As Integer End Function
If you are using vb.net all you need to do is use ...
We have built in support for GetEnvironmentVariable in VB.NET and C#

dim myWindir as string = System.Environment.GetEnvironmentVariable("windir")

or

dim myProgramFiles as string = System.Environment.GetEnvironmentVariable("programfiles")


if you want to grab just %systemroot% then use

dim

RootDir AsString = System.Environment.SystemDirectory

Happy Coding
Louis
http://otggroup.com