Environ Environ Functions Named Arguments

Syntax

Environ({envstring | number}) envstring

Key: Optional

Data Type: String

The name of the required environment variable.

number

Key: Optional

Data Type: Numeric expression

The ordinal number of the environment variable within the environment string table.

Return Value

Environ returns a string containing the text assigned to envstring. Description

Returns the value assigned to an operating-system environment variable. Rules at a Glance

• A zero-length string ("") is returned if envstring doesn't exist in the operating system's environment-string table, or if there is no environment string in the position specified by number.

• envstring and number are mutually exclusive; that is, you can specify one or the other, but not both.

248 Chapter 7- The Language Reference

Example

Private Type env strVarName As String strValue As String End Type

Private Sub Form_Load()

Dim intCtr As Integer, intPos As Integer Dim strRetVal As String Dim udtEnv As env intCtr = 1 Do strRetVal = Environ(intCtr) If strRetVal <> "" Then intPos = InStr(1, strRetVal, "=") udtEnv.strVarName = Left(strRetVal, intPos - 1) udtEnv.strValue = Mid(strRetVal, intPos + 1) Else

Exit Do End If intCtr = intCtr + 1 Loop

End Sub Programming Tips & Gotchas

• If you use number to specify the environment variable, both the name and the value of the variable are returned. An equals sign (=) separates them. For example, the function call Environ(l) might return the string TEMP=C:\WIN-DOWS\TEMP.

• If you retrieve environment variables and their values by ordinal position, the first variable is in position 1, not position 0.

• Due to the flexibility offered, it's now accepted and recommended practice to use the registry for variables needed by your application, rather than the environment string table.

• Environment variables can be defined in a variety of ways, including by the AUTOEXEC.BAT and MSDOS.SYS files, as well as by the HKEY_LOCAL_ MACHINE\System\CurrentControlSet\Control\SessionManager\Envi-ronment and HKEY_CURRENT_USER\Environment keys in the registry. However, the Environ function doesn't recognize environment variables defined in the registry.

0 0

Post a comment

  • Receive news updates via email from this site