GetEnvironmentVariable Function

Declare Function GetEnvironmentVariable Lib "kernel32.dll" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long

Platforms

Description & Usage

GetEnvironmentVariable reads the value of one of the computer's environment variables. The value is placed into the string buffer passed as the lpBuffer parameter.

Return Value

If the specified environment variable does not exist, the function returns zero. If the string passed as lpBuffer was too short to receive the environment variable's value, the function returns the necessary minimum buffer length. If successful, the function returns the length of the string copied into lpBuffer, not counting the terminating null character.

Visual Basic-Specific Issues

None.

Parameters

lpName
The name of the environment variable to read.
lpBuffer
Receives the value of the environment variable. This string must be sufficiently long to receive the value copied into it.
nSize
The length of the string passed as lpBuffer.

Example

Read the BLASTER environment variable, which contains some information about the Sound Blaster configuration on the computer. Typically, this variable is only used by DOS programs, so it isn't very useful for Windows applications. But then again, this is just an example of how to use GetEnvironmentVariable. The example runs when button Command1 is clicked, so you must place a command button named Command1 on a form window before running this example.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function GetEnvironmentVariable Lib "kernel32.dll" Alias "GetEnvironmentVariableA" (ByVal _
	lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long

' *** Place the following code inside a form window. ***

Private Sub Command1_Click()
	Dim envvar As String  ' receives the value of the environment variable
	Dim slength As Long  ' length of the string copied into envvar
	
	' Make enough room in envvar to receive the data.
	envvar = Space(256)
	' Read the value of the BLASTER environment variable.
	slength = GetEnvironmentVariable("BLASTER", envvar, Len(envvar))
	
	If slength = 0 Then
		Debug.Print "The BLASTER environment variable does not exist on this system."
	Else
		' Remove the terminating null and everything following it.
		envvar = Left(envvar, slength)
		Debug.Print "BLASTER = "; envvar
	End If
End Sub

See Also

SetEnvironmentVariable

Category

Processes & Threads

Back to the Function list.
Back to the Reference section.


Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getenvironmentvariable.html