A partial answer to your first question: This snippet returns all shared resources on your network. Actually more than you asked for, but it isn't hard to filter out all but the computer names. You will need a list box, a command button and a check box. The value of the check box determines whether the list box shows all resources or just the computer names. Use this code to show the computer names and then see thread711-47273 for a small discussion on shutting down remote workstations.
[tt]Option Explicit
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCE_REMEMBERED = &H3
Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Private Const RESOURCEDISPLAYTYPE_FILE = &H4
Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Private Const RESOURCEDISPLAYTYPE_GROUP = &H5
Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFF
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const RESOURCEUSAGE_RESERVED = &H80000000
Private Const NO_ERROR = 0
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Private Type NETRESOURCEWSTRINGS
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lLocalName As Long
lRemoteName As Long
lComment As Long
lProvider As Long
bByteArray(0 To 1024) As Byte
End Type
Private Declare Function VBlstrcpy Lib "kernel32.dll" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function VBlstrlen Lib "kernel32.dll" Alias "lstrlenA" _
(ByVal lpString As Long) As Long
Private Declare Function WNetOpenEnum Lib "mpr.dll" _
Alias "WNetOpenEnumA" _
(ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As NETRESOURCE, _
lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" _
Alias "WNetEnumResourceA" (ByVal hEnum As Long, _
lpcCount As Long, lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" _
(ByVal hEnum As Long) As Long
Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" _
Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, _
ByVal lpMessage As String, ByVal dwTimeout As Long, _
ByVal bForceAppsClosed As Long, _
ByVal bRebootAfterShutdown As Long) As Long
Dim TabCount As Integer
Private Function EnumerateFunc(nrPassed As NETRESOURCEWSTRINGS) As Boolean
Dim dwResult As Long, dwResultEnum As Long
Dim hEnum As Long, cEntries As Long
Dim nrLocal As NETRESOURCEWSTRINGS, nrInfo As NETRESOURCE
Dim tLocalName As String, tRemoteName As String
TabCount = TabCount + 1
With nrInfo
.dwScope = nrPassed.dwScope
.dwType = nrPassed.dwType
.dwDisplayType = nrPassed.dwDisplayType
.dwUsage = nrPassed.dwUsage
.lpLocalName = ParseResourceString(nrPassed.lLocalName)
.lpRemoteName = ParseResourceString(nrPassed.lRemoteName)
.lpComment = ParseResourceString(nrPassed.lComment)
.lpProvider = ParseResourceString(nrPassed.lProvider)
End With
cEntries = 1 '
enumerate entries one at a time
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, _
RESOURCETYPE_ANY, 0, nrInfo, hEnum)
If Not (dwResult = NO_ERROR) Then
'
An error occurred. Add code to determine what happened.
EnumerateFunc = False
TabCount = TabCount - 1
Exit Function
End If
Do
dwResultEnum = WNetEnumResource(hEnum, _
cEntries, nrLocal, Len(nrLocal))
If (dwResultEnum = NO_ERROR) Then
tRemoteName = ParseResourceString(nrLocal.lRemoteName)
If Check1.Value = 1 Then 'list all resources
List1.AddItem String(TabCount, vbTab) & tRemoteName
Else
If TabCount = 2 Then
'list only the computers....
'You will probably have to adjust this value depending
'on the machine where the code is run. Normally, the first
'entry would be a workgroup name, showing as a container
'for workstations, serving as containers for various shared resources.
'On a domain contoller, the first entry may show as the network provider,
'i.e. "Microsoft Windows Network", followed by workgroups, etc.
List1.AddItem tRemoteName
End If
End If
'
If this NETRESOURCE is a container,
' call the function recursively.
If (RESOURCEUSAGE_CONTAINER = (nrLocal.dwUsage _
And RESOURCEUSAGE_CONTAINER)) _
Then
If Not (EnumerateFunc(nrLocal)) Then
DoEvents
End If
End If
ElseIf Not (dwResultEnum = ERROR_NO_MORE_ITEMS) Then
'
An error occurred. Handle it here...
Exit Do
End If
Loop While Not (dwResultEnum = ERROR_NO_MORE_ITEMS)
dwResult = WNetCloseEnum(hEnum)
If Not (dwResult = NO_ERROR) Then
'
An error occurred. Handle it here...
EnumerateFunc = False
TabCount = TabCount - 1
Exit Function
End If
EnumerateFunc = True
TabCount = TabCount - 1
End Function
Function ParseResourceString(lStringPtrIn As Long) As String
Dim sTmp As String
Dim lCount As Long
lCount = VBlstrlen(lStringPtrIn)
If lCount > 0 Then
sTmp = String$(255, 0)
VBlstrcpy sTmp, lStringPtrIn
ParseResourceString = Left(sTmp, lCount)
Else
ParseResourceString = ""
End If
End Function
Private Sub Command1_Click()
Dim NRCS As NETRESOURCEWSTRINGS
Dim bFinished As Boolean
List1.Clear
bFinished = EnumerateFunc(NRCS)
End Sub
Private Sub Form_Load()
Command1.Caption = "List Network Resources"
Check1.Caption = "Show All Resources"
End Sub[/tt]
Alt255@Vorpalcom.Intranets.com
"If you can get people to ask the wrong questions, they'll never find the right answers."[tt]
Thomas Pynchon[/tt]
Perhaps the reverse is also true....