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 |
|
|
Library |
|
|
DLL |
|
|
Unicode and ANSI names | GetEnvironmentVariableW (Unicode) and GetEnvironmentVariableA (ANSI) |
See also
Send comments about this topic to Microsoft
Build date: 3/7/2012
- 7/14/2010
- User1024x768
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
- 5/18/2009
- Thomas Lee
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
<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
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.SystemDirectoryHappy Coding
Louis
http://otggroup.com
- 4/6/2009
- LouSha