Process and Thread Function ...


GetEnvironmentVariable Function

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

Syntax

C++
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, 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.

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 clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesGetEnvironmentVariableW (Unicode) and GetEnvironmentVariableA (ANSI)

See Also

Environment Variables
GetEnvironmentStrings
SetEnvironmentVariable

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags :


Community Content

dmex
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
Tags : vb.net syntax

LouSha
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
Tags :

dmex
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
Tags : c# syntax

Thomas Lee
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


Page view tracker