کد نمونه:
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.
' 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