Link to home
Start Free TrialLog in
Avatar of Gitcho
Gitcho

asked on

How to return an array/dict.obj from function

1. what's the best method to use ? array or dictionary object
2. how would I code it (see function below) to add the name of each computer to the array
3. how would I loop thru the array once the function returns it ?


Function getHostNameFromUsername(sUsername)

      Dim objUser,userClass,adsComputer,adsSession,HomeDir,dPClist

      On Error Resume Next

      Set objUser= GetObject("WinNT://Domain/" & sUserName & ",user")
      Set userClass = GetObject(objUser.Schema)


      ' **********************************************************************
      ' Return false if bad username
      ' **********************************************************************
      If objUser.name = "" Then
            getHostNameFromUsername = False
      End If


      ' **********************************************************************
      ' Get Logon server from profile dir
      ' **********************************************************************
      Dim pos1,pos2
      HomeDir = objUser.HomeDirectory
      pos1 = InStrRev(objUser.HomeDirectory, "\")
      HomeDir = Mid(objUser.HomeDirectory,3,pos1-3)


      ' **********************************************************************
      ' Identifying which sessions exist on a machine
      ' **********************************************************************
      Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
      
      For Each adsSession In adsComputer.Sessions
            *** Want to add the name of each computer a user is logged onto to array ****
            *** code to add "adsSession.Computer" to [whatever_array] goes here ***
      Next

      On Error Goto 0
      
      *** Want to return the array of computers a user is logged onto ****
      getHostNameFromUsername = [whatever_array]

End Function
Avatar of JR2003
JR2003

To use a sripting dictionary set a reference in the menu option Project/References to "Microsoft Scripting Runtime"
The function has been changed to return a Scripting.Dictionary object so setting the reult to false has been commented out. You could always passs the scripting dicrionary reference as a parameter rather than the return valu of the function.

Option Explicit

Function getHostNameFromUsername(sUsername) As Scripting.Dictionary

     Dim objUser, userClass, adsComputer, adsSession, HomeDir, dPClist

     Dim colComputers As Scripting.Dictionary
     
     On Error Resume Next

     Set objUser = GetObject("WinNT://Domain/" & sUsername & ",user")
     Set userClass = GetObject(objUser.Schema)


     ' **********************************************************************
     ' Return false if bad username
     ' **********************************************************************
     If objUser.Name = "" Then
          'getHostNameFromUsername = False
     End If


     ' **********************************************************************
     ' Get Logon server from profile dir
     ' **********************************************************************
     Dim pos1, pos2
     HomeDir = objUser.HomeDirectory
     pos1 = InStrRev(objUser.HomeDirectory, "\")
     HomeDir = Mid(objUser.HomeDirectory, 3, pos1 - 3)


     ' **********************************************************************
     ' Identifying which sessions exist on a machine
     ' **********************************************************************
     Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
     Set colComputers = New Scripting.Dictionary
     For Each adsSession In adsComputer.Sessions
          colComputers.Add CStr(adsSession.Computer), CStr(adsSession.Computer)
          '*** Want to add the name of each computer a user is logged onto to array ****
          '*** code to add "adsSession.Computer" to [whatever_array] goes here ***
     Next

     On Error GoTo 0
     
     '*** Want to return the array of computers a user is logged onto ****
     Set getHostNameFromUsername = colComputers

End Function
To loop through the collection:

Private Sub Command1_Click()

    Dim MyComputers As Scripting.Dictionary
    Dim i As Long
    Set MyComputers = getHostNameFromUsername()
    With MyComputers
        For i = 0 To .Count - 1
            MsgBox .Items(i)
        Next i
    End With
   
End Sub
To do it using an array:

Function getHostNameFromUsername(sUsername) As String()

     Dim objUser, userClass, adsComputer, adsSession, HomeDir, dPClist

     Dim arrComputers() As String
     
     On Error Resume Next

     Set objUser = GetObject("WinNT://Domain/" & sUsername & ",user")
     Set userClass = GetObject(objUser.Schema)


     ' **********************************************************************
     ' Return false if bad username
     ' **********************************************************************
     If objUser.Name = "" Then
          'getHostNameFromUsername = False
     End If


     ' **********************************************************************
     ' Get Logon server from profile dir
     ' **********************************************************************
     Dim pos1, pos2
     HomeDir = objUser.HomeDirectory
     pos1 = InStrRev(objUser.HomeDirectory, "\")
     HomeDir = Mid(objUser.HomeDirectory, 3, pos1 - 3)


     ' **********************************************************************
     ' Identifying which sessions exist on a machine
     ' **********************************************************************
     Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
     Dim i As Long
     i = 0
     For Each adsSession In adsComputer.Sessions
          ReDim Preserve arrComputers(i)
          arrComputers(i) = CStr(adsSession.Computer)
          '*** Want to add the name of each computer a user is logged onto to array ****
          '*** code to add "adsSession.Computer" to [whatever_array] goes here ***
     Next

     On Error GoTo 0
     
     '*** Want to return the array of computers a user is logged onto ****
     getHostNameFromUsername = arrComputers

End Function


Private Sub Command1_Click()

    Dim arrComputers() As String
    Dim i As Long
    arrComputers = getHostNameFromUsername()
        For i = LBound(arrComputers) To UBound(arrComputers)
            MsgBox arrComputers(i)
        Next i
   
End Sub
Avatar of Gitcho

ASKER

Good stuff ... what's your recommendation - dictionary or array ? What does best practises say to use ?
Avatar of Gitcho

ASKER

Shoot ... sorry ... forgot to mention this is for an ASP page ... doesn't look like you can typecast "Dim arrComputers() As String" in vbscript ...

Any way you can still make that work ?
I would probably use an array in this circumstance as they are simpler. But that's not to say there's anything wrong with using a dictionary object.
ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gitcho

ASKER

This was the code that ended up working on my page ...  thanks ...

-----------------------------------------------------------------------------------

Function getHostNameFromUsername(sUsername)

      Dim objUser,userClass,adsComputer,adsSession,HomeDir,colComputers

      Set objUser= GetObject("WinNT://Domain/" & sUserName & ",user")
      Set userClass = GetObject(objUser.Schema)
      
      
      ' **********************************************************************
      ' Return false if bad username
      ' **********************************************************************
      If objUser.name = "" Then
            getHostNameFromUsername = False
      End If


      ' **********************************************************************
      ' Get Logon server from profile dir
      ' **********************************************************************      
      Dim pos1,pos2
      HomeDir = objUser.HomeDirectory
      pos1 = InStrRev(objUser.HomeDirectory, "\")
      HomeDir = Mid(objUser.HomeDirectory,3,pos1-3)


      ' **********************************************************************
      ' Identifying which sessions exist on a machine
      ' **********************************************************************
      
      Set adsComputer = GetObject("WinNT://" & HomeDir & "/LanManServer")
      Set colComputers = CreateObject("Scripting.Dictionary")

      For Each adsSession In adsComputer.Sessions
            If uCase(adsSession.User) = uCase(sUsername) Then
                  colComputers.Add CStr(adsSession.Computer), CStr(adsSession.Computer)
            End If
      Next

      Set getHostNameFromUsername = colComputers

End Function