-
I'm working across a Windows 95/98 Network and I need a way to determine the usernames of the other computers across the network. There must be a way to do this as it is implimented in the WinPopup program by Microsoft. If somebody could help me out with the API calls or similar that would be great.
Thanks
-
Hi,
Try this:
Code:
Option Explicit
Public NetworkDrives() As String
Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCE_REMEMBERED As Long = &H3&
Private Const RESOURCEDISPLAYTYPE_DIRECTORY& = &H9
Private Const RESOURCEDISPLAYTYPE_DOMAIN& = &H1
Private Const RESOURCEDISPLAYTYPE_FILE& = &H4
Private Const RESOURCEDISPLAYTYPE_GENERIC& = &H0
Private Const RESOURCEDISPLAYTYPE_GROUP& = &H5
Private Const RESOURCEDISPLAYTYPE_NETWORK& = &H6
Private Const RESOURCEDISPLAYTYPE_ROOT& = &H7
Private Const RESOURCEDISPLAYTYPE_SERVER& = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEDISPLAYTYPE_SHAREADMIN& = &H8
Private Const RESOURCETYPE_ANY As Long = &H0&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCETYPE_PRINT As Long = &H2&
Private Const RESOURCETYPE_UNKNOWN As Long = &HFFFF&
Public Const RESOURCEUSAGE_ALL As Long = &H0&
Public Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
Public Const RESOURCEUSAGE_CONTAINER As Long = &H2&
Public Const RESOURCEUSAGE_RESERVED As Long = &H80000000
Public Const NO_ERROR = 0
Public Const ERROR_MORE_DATA = 234
Public Const RESOURCE_ENUM_ALL As Long = &HFFFF
Public Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
pLocalName As Long
pRemoteName As Long
pComment As Long
pProvider As Long
End Type
Public Type NETRESOURCE_REAL
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
sLocalName As String
sRemoteName As String
sComment As String
sProvider As String
End Type
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Public Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
Public Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpBuffer As NETRESOURCE, lpBufferSize As Long) As Long
Public Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
Public Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject As Any) As Long
Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo As Any, lpFrom As Any, ByVal lLen As Long)
Public Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpTo As Long, ByVal lpFrom As Long, ByVal lLen As Long)
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Any) As Long
Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Public Sub GetNetworkDrives()
Const MAX_RESOURCES = 256
Const NOT_A_CONTAINER = -1
Dim bFirstTime As Boolean
Dim lReturn As Long
Dim hEnum As Long
Dim lCount As Long
Dim lMin As Long
Dim lLength As Long
Dim l As Long
Dim lBufferSize As Long
Dim lLastIndex As Long
Dim uNetApi(0 To MAX_RESOURCES) As NETRESOURCE
Dim uNet() As NETRESOURCE_REAL
Dim CurrentDomain
Dim iCount As Integer
iCount = 0
bFirstTime = True 'Make code below aware that that it is the first loop
Do
DoEvents
If bFirstTime Then 'If this is the first time the loop has executed then
'Start the enumeration of all network resources
lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, ByVal 0&, hEnum) 'Get a handle to a buffer
'The next time it reaches the if bfirsttime then line it will not be the first time
bFirstTime = False
Else
If uNet(lLastIndex).dwUsage And RESOURCEUSAGE_CONTAINER Then
lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, uNet(lLastIndex), hEnum) 'Initiate the enumeration process
Else
lReturn = NOT_A_CONTAINER 'Set the returnvalue to NOT_A_CONTAINER for later reference
hEnum = 0
End If
lLastIndex = lLastIndex + 1 'Increase the network resource number by 1 (array number)
End If
If lReturn = NO_ERROR Then 'If there was no error then procress the data retrieved
lCount = RESOURCE_ENUM_ALL 'Enumerate all resoureces
Do
DoEvents
lBufferSize = UBound(uNetApi) * Len(uNetApi(0)) / 2 'Set the buffer size
lReturn = WNetEnumResource(hEnum, lCount, uNetApi(0), lBufferSize) 'Get resource information
If lCount > 0 Then
'The next bit copys the information from memory to uNetApi array
ReDim Preserve uNet(0 To lMin + lCount - 1) As NETRESOURCE_REAL
For l = 0 To lCount - 1
uNet(lMin + l).dwScope = uNetApi(l).dwScope
uNet(lMin + l).dwType = uNetApi(l).dwType
uNet(lMin + l).dwDisplayType = uNetApi(l).dwDisplayType
uNet(lMin + l).dwUsage = uNetApi(l).dwUsage
If uNetApi(l).pLocalName Then
lLength = lstrlen(uNetApi(l).pLocalName)
uNet(lMin + l).sLocalName = Space$(lLength)
CopyMem ByVal uNet(lMin + l).sLocalName, ByVal uNetApi(l).pLocalName, lLength
End If
If uNetApi(l).pRemoteName Then
lLength = lstrlen(uNetApi(l).pRemoteName)
uNet(lMin + l).sRemoteName = Space$(lLength)
CopyMem ByVal uNet(lMin + l).sRemoteName, ByVal uNetApi(l).pRemoteName, lLength
End If
If uNetApi(l).pComment Then
lLength = lstrlen(uNetApi(l).pComment)
uNet(lMin + l).sComment = Space$(lLength)
CopyMem ByVal uNet(lMin + l).sComment, ByVal uNetApi(l).pComment, lLength
End If
If uNetApi(l).pProvider Then
lLength = lstrlen(uNetApi(l).pProvider)
uNet(lMin + l).sProvider = Space$(lLength)
CopyMem ByVal uNet(lMin + l).sProvider, ByVal uNetApi(l).pProvider, lLength
End If
Next l
End If
lMin = lMin + lCount
Loop While lReturn = ERROR_MORE_DATA
End If
If hEnum Then l = WNetCloseEnum(hEnum)
Loop While lLastIndex < lMin
If UBound(uNet) > 0 Then
For l = 0 To UBound(uNet)
DoEvents
'Select what type of resource it is (there are more but these are the only ones that we want
Select Case uNet(l).dwDisplayType
Case RESOURCEDISPLAYTYPE_DOMAIN
CurrentDomain = uNet(l).sRemoteName
Case RESOURCEDISPLAYTYPE_SERVER
iCount = iCount + 1
ReDim Preserve NetworkDrives(iCount)
If CurrentDomain <> "" Then
NetworkDrives(iCount) = Mid$(uNet(l).sRemoteName, 3, Len(uNet(l).sRemoteName) - 2)
Else
NetworkDrives(iCount) = Mid$(uNet(l).sRemoteName, 3, Len(uNet(l).sRemoteName) - 2)
End If
End Select
Next l
End If
End Sub
It returns the names of all available network drives in an array (NetworkDrives).
Hope this helps
Shaun
-
What I want to be able to do is pass a computer name into a function and it will return the username of that computer. For example if a computer is named Computer1 and the user is logged on as person1 when I call a function GetUserName("Computer1") it will return "person1".
-
Please help me as I need to get my program finished and I need to be able to work this out.