VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "EVClass"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Option Explicit

#If Win32 Then
Private Declare Function GetEnvironmentStrings& Lib "kernel32" Alias "GetEnvironmentStringsA" ()
Private Declare Function GetEnvironmentVariable& Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long)
Private Declare Function SetEnvironmentVariable& Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String)
Private Declare Function ExpandEnvironmentStrings& Lib "kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long)
Private Declare Function FreeEnvironmentStrings Lib "kernel32" Alias "FreeEnvironmentStringsA" (ByVal lpsz As String) As Long
#End If 'WIN32

Private datEVBlock&
'Extracts the idx%'th string from source$, where the
'substrings are separated by character sep$
'idx%=0 is the first string
Function ParseAnyString$(source$, ByVal idx%, ByVal sep$)
    Dim nexttab%, basepos%, thispos%
    Dim res$
    basepos% = 1
    thispos% = 0
    If (Len(source$) = 0) Then
        ParseAnyString$ = ""
        Exit Function
    End If
    Do
        nexttab% = InStr(basepos%, source$, sep$)
        If nexttab% = 0 Then nexttab% = Len(source$) + 1
        'Now points to next tab or 1 past end of string
        'The following should never happen
        'If nexttab% = basepos% Then GoTo ptsloop1

        If thispos% = idx% Then
            If nexttab% - basepos% - 1 < 0 Then
                res$ = ""
            Else
                res$ = Mid$(source$, basepos%, nexttab% - basepos%)
            End If
            Exit Do
        End If
ptsloop1:
        basepos% = nexttab% + 1
        thispos% = thispos% + 1
    Loop While (basepos% <= Len(source$))
    ParseAnyString$ = res$
End Function

Private Sub Class_Initialize()
    datEVBlock& = GetEnvironmentStrings&()
End Sub


Private Sub Class_Terminate()
    Dim dl&
    dl& = FreeEnvironmentStrings(datEVBlock)
End Sub



'
' Determine the number of strings
Public Function GetStringCount%()
    Dim e$, e2$
    Dim strcnt%
    e$ = agGetStringFrom2NullBuffer(datEVBlock)
    Do
        e2$ = ParseAnyString(e$, strcnt%, Chr$(0))
        Debug.Print e2$
        If e2$ <> "" Then strcnt% = strcnt% + 1
    Loop While e2$ <> ""
    GetStringCount = strcnt%
End Function

Public Function GetString$(idx%)
    Dim e$, e2$
    Dim startpos%, newpos%
    e$ = agGetStringFrom2NullBuffer(datEVBlock)
    e2$ = ParseAnyString(e$, idx%, Chr$(0))
    If Left$(e2$, 1) = "=" Then startpos% = 2 Else startpos% = 1
    newpos% = InStr(startpos%, e2$, "=")
    If newpos% > 0 Then e2$ = Left$(e2$, newpos% - 1)
    GetString$ = e2$
End Function

'
' Retreives the value of an environment string
'
Public Function GetStringValue$(ByVal envstr$)
    Dim dl&
    Dim buf$
    buf$ = String$(2048, 0)
    dl& = GetEnvironmentVariable(envstr$, buf$, 2047)
    If dl& > 0 Then GetStringValue$ = Left$(buf$, dl&)
End Function
