Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
 GetEnvironmentVariable Function

  Switch on low bandwidth view
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: 7/2/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
vb.net syntax      Fazalbhi ... dmex   |   Edit   |   Show History
<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
Flag as ContentBug
If you are using vb.net all you need to do is use ...      LouSha   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
Tags What's this?: c# (x) syntax (x) Add a tag
Flag as ContentBug
Alternatives to this unmanaged API      Thomas Lee   |   Edit   |   Show History

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

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker