Private Declare Function GetEnvironmentStrings Lib "kernel32" Alias "GetEnvironmentStringsA" () As Long Private Declare Function FreeEnvironmentStrings Lib "kernel32" Alias "FreeEnvironmentStringsA" (ByVal lpsz As String) As Long Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long) Private Sub Form_Load() 'The KPD-Team 2001 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim lngRet As Long, strDest As String, lLen As Long 'set the graphics mode to persistent Me.AutoRedraw = True 'retrieve the initial pointer to the environment strings lngRet = GetEnvironmentStrings Do 'get the length of the following string lLen = lstrlen(lngRet) 'if the length equals 0, we've reached the end If lLen = 0 Then Exit Do 'create a buffer string strDest = Space$(lLen) 'copy the text from the environment block CopyMemory ByVal strDest, ByVal lngRet, lLen 'show the text Me.Print strDest 'move the pointer lngRet = lngRet + lstrlen(lngRet) + 1 Loop 'clean up FreeEnvironmentStrings lngRet End Sub
在VB里有 Environ 函数,直接用它才是VB程序员的风格。 Environ 函数示例 本示例使用 Environ 函数来提供来自环境变量表中 PATH 语句的长度及路径项目数。Dim EnvString, Indx, Msg, PathLen ' 声明变量。 Indx = 1 ' 设置索引值的初值为 1。 Do EnvString = Environ(Indx) ' 取得环境变量。 If Left(EnvString, 5) = "PATH=" Then ' 检查 PATH 项。 PathLen = Len(Environ("PATH")) ' 取得长度。 Msg = "PATH entry = " & Indx & " and length = " & PathLen Exit Do Else Indx = Indx + 1 ' 不是 PATH 项, End If ' 则跳过此项,继续检查下一项。 Loop Until EnvString = "" If PathLen > 0 Then MsgBox Msg ' 显示消息。 Else MsgBox "No PATH environment variable exists." End If
Private Declare Function FreeEnvironmentStrings Lib "kernel32" Alias "FreeEnvironmentStringsA" (ByVal lpsz As String) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Sub Form_Load()
'The KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim lngRet As Long, strDest As String, lLen As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'retrieve the initial pointer to the environment strings
lngRet = GetEnvironmentStrings
Do
'get the length of the following string
lLen = lstrlen(lngRet)
'if the length equals 0, we've reached the end
If lLen = 0 Then Exit Do
'create a buffer string
strDest = Space$(lLen)
'copy the text from the environment block
CopyMemory ByVal strDest, ByVal lngRet, lLen
'show the text
Me.Print strDest
'move the pointer
lngRet = lngRet + lstrlen(lngRet) + 1
Loop
'clean up
FreeEnvironmentStrings lngRet
End Sub
Environ 函数示例
本示例使用 Environ 函数来提供来自环境变量表中 PATH 语句的长度及路径项目数。Dim EnvString, Indx, Msg, PathLen ' 声明变量。
Indx = 1 ' 设置索引值的初值为 1。
Do
EnvString = Environ(Indx) ' 取得环境变量。
If Left(EnvString, 5) = "PATH=" Then ' 检查 PATH 项。
PathLen = Len(Environ("PATH")) ' 取得长度。
Msg = "PATH entry = " & Indx & " and length = " & PathLen
Exit Do
Else
Indx = Indx + 1 ' 不是 PATH 项,
End If ' 则跳过此项,继续检查下一项。
Loop Until EnvString = ""
If PathLen > 0 Then
MsgBox Msg ' 显示消息。
Else
MsgBox "No PATH environment variable exists."
End If