limct
asked on
Why list the servers with code "NetServerEnum" works perfect on one machine but not the other?
Here my code I copied from one web site which works perfect on my machine but when run on other machines it only picked up some servers but not all?
I have tried: Public Const SV_TYPE_SERVER = &H2
Public Const SV_TYPE_ALL As Long = &HFFFFFFFF
Please give answer with code. Thank you.
Function GetServerInfo()
Dim lReturnCode As Long
Dim bBuffer(512) As Byte
Dim i As Integer, x As Integer
Dim tSeverInfo101 As SERVER_INFO_101, lSeverInfo101 As Long
Dim sComputerName As String
Dim lPreferedMaxLen As Long
Dim lEntriesRead As Long
Dim lTotalEntries As Long
Dim lServerType As Long
Dim sDomain As String
Dim vResume As Variant
Dim lSeverInfo101StructPtr As Long
'Clear all of the sComputerName
sComputerName = ""
lServerType = SV_TYPE_SERVER
'Call NetServerEnum to get a list of Servers
lReturnCode = NetServerEnum(ByVal 0&, 101, lSeverInfo101, lPreferedMaxLen, lEntriesRead, lTotalEntries, ByVal lServerType, ByVal sDomain, vResume)
' NetServerEnum Index is 1 based
x = 1
lSeverInfo101StructPtr = lSeverInfo101
Do While x <= lTotalEntries
RtlMoveMemory tSeverInfo101, ByVal lSeverInfo101StructPtr, Len(tSeverInfo101)
lstrcpyW bBuffer(0), tSeverInfo101.wki101_serve rname
'Get every other byte from Unicode string.
i = 0
Do While bBuffer(i) <> 0
sComputerName = sComputerName & Chr(bBuffer(i))
i = i + 2
Loop
Combo_SQLServer.AddItem sComputerName
'Debug.Print "Found server: " & sComputerName
sComputerName = ""
x = x + 1
lSeverInfo101StructPtr = lSeverInfo101StructPtr + Len(tSeverInfo101)
Loop
lReturnCode = NetApiBufferFree(lSeverInf o101)
End Function
I have tried: Public Const SV_TYPE_SERVER = &H2
Public Const SV_TYPE_ALL As Long = &HFFFFFFFF
Please give answer with code. Thank you.
Function GetServerInfo()
Dim lReturnCode As Long
Dim bBuffer(512) As Byte
Dim i As Integer, x As Integer
Dim tSeverInfo101 As SERVER_INFO_101, lSeverInfo101 As Long
Dim sComputerName As String
Dim lPreferedMaxLen As Long
Dim lEntriesRead As Long
Dim lTotalEntries As Long
Dim lServerType As Long
Dim sDomain As String
Dim vResume As Variant
Dim lSeverInfo101StructPtr As Long
'Clear all of the sComputerName
sComputerName = ""
lServerType = SV_TYPE_SERVER
'Call NetServerEnum to get a list of Servers
lReturnCode = NetServerEnum(ByVal 0&, 101, lSeverInfo101, lPreferedMaxLen, lEntriesRead, lTotalEntries, ByVal lServerType, ByVal sDomain, vResume)
' NetServerEnum Index is 1 based
x = 1
lSeverInfo101StructPtr = lSeverInfo101
Do While x <= lTotalEntries
RtlMoveMemory tSeverInfo101, ByVal lSeverInfo101StructPtr, Len(tSeverInfo101)
lstrcpyW bBuffer(0), tSeverInfo101.wki101_serve
'Get every other byte from Unicode string.
i = 0
Do While bBuffer(i) <> 0
sComputerName = sComputerName & Chr(bBuffer(i))
i = i + 2
Loop
Combo_SQLServer.AddItem sComputerName
'Debug.Print "Found server: " & sComputerName
sComputerName = ""
x = x + 1
lSeverInfo101StructPtr = lSeverInfo101StructPtr + Len(tSeverInfo101)
Loop
lReturnCode = NetApiBufferFree(lSeverInf
End Function
Did you run this on seperate machines with the same OS? The code looks ok. maybe machines configurations affects the return value. You may check what protocols are available in ur machine and compair it with the other. Or try to check if the machine has an access to the server. I cant really see from here where the problem is. But i hope i helped. :)
Option Explicit
'Windows type used to call the Net API
' Some of the folowing declarations are not completely needed for ur case
'------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_DOMAIN_NAME_LEN As Long = 128
Private Const MAX_HOSTNAME_LEN As Long = 128
Private Const MAX_SCOPE_ID_LEN As Long = 256
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000 'Windows95 and above
Private Const SV_TYPE_DFS As Long = &H800000 'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000 'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000 'rtn alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000 'rtn local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const SV_PLATFORM_ID_OS2 As Long = 400
Private Const SV_PLATFORM_ID_NT As Long = 500
Private Const MAJOR_VERSION_MASK As Long = &HF
Private Type SERVER_INFO_100
sv100_platform_id As Long
sv100_name As Long
End Type
Private Declare Function NetServerEnum Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
ByVal servertype As Long, _
ByVal domain As Long, _
resume_handle As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Function GetServers(ByVal sDomain As String) As Long
On Error GoTo Last:
'lists all servers of the specified type
'that are visible in a domain.
Dim bufptr As Long
Dim dwEntriesread As Long
Dim dwTotalentries As Long
Dim dwResumehandle As Long
Dim se100 As SERVER_INFO_100
Dim success As Long
Dim nStructSize As Long
Dim cnt As Long
nStructSize = LenB(se100)
success = NetServerEnum(0&, _
100, _
bufptr, _
MAX_PREFERRED_LENGTH, _
dwEntriesread, _
dwTotalentries, _
SV_TYPE_ALL, _
StrPtr(sDomain), _
dwResumehandle)
'if all goes well
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
'loop through the returned data, adding each
'machine to the list
For cnt = 0 To dwEntriesread - 1
'get one chunk of data and cast
'into an SERVER_INFO_100 struct
'in order to add the name to a list
CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize
'Listing Servers
'----------------
Debug.Print GetPointerToByteStringW(se 100.sv100_ name)
Next
End If
'clean up regardless of success
Call NetApiBufferFree(bufptr)
'return entries as sign of success
GetServers = dwEntriesread
Exit Function
Last:
Debug.Print "Enumerating Servers Failed"
Err.Clear
End Function
Public Function GetPointerToByteStringW(By Val dwData As Long) As String
Dim tmp() As Byte
Dim tmplen As Long
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp
End If
End If
End Function
'Just call the function GetServers
' and check now all servers are get listed
;-)
Shiju
'Windows type used to call the Net API
' Some of the folowing declarations are not completely needed for ur case
'-------------------------
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_DOMAIN_NAME_LEN As Long = 128
Private Const MAX_HOSTNAME_LEN As Long = 128
Private Const MAX_SCOPE_ID_LEN As Long = 256
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000 'Windows95 and above
Private Const SV_TYPE_DFS As Long = &H800000 'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000 'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000 'rtn alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000 'rtn local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const SV_PLATFORM_ID_OS2 As Long = 400
Private Const SV_PLATFORM_ID_NT As Long = 500
Private Const MAJOR_VERSION_MASK As Long = &HF
Private Type SERVER_INFO_100
sv100_platform_id As Long
sv100_name As Long
End Type
Private Declare Function NetServerEnum Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
ByVal servertype As Long, _
ByVal domain As Long, _
resume_handle As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Function GetServers(ByVal sDomain As String) As Long
On Error GoTo Last:
'lists all servers of the specified type
'that are visible in a domain.
Dim bufptr As Long
Dim dwEntriesread As Long
Dim dwTotalentries As Long
Dim dwResumehandle As Long
Dim se100 As SERVER_INFO_100
Dim success As Long
Dim nStructSize As Long
Dim cnt As Long
nStructSize = LenB(se100)
success = NetServerEnum(0&, _
100, _
bufptr, _
MAX_PREFERRED_LENGTH, _
dwEntriesread, _
dwTotalentries, _
SV_TYPE_ALL, _
StrPtr(sDomain), _
dwResumehandle)
'if all goes well
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
'loop through the returned data, adding each
'machine to the list
For cnt = 0 To dwEntriesread - 1
'get one chunk of data and cast
'into an SERVER_INFO_100 struct
'in order to add the name to a list
CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize
'Listing Servers
'----------------
Debug.Print GetPointerToByteStringW(se
Next
End If
'clean up regardless of success
Call NetApiBufferFree(bufptr)
'return entries as sign of success
GetServers = dwEntriesread
Exit Function
Last:
Debug.Print "Enumerating Servers Failed"
Err.Clear
End Function
Public Function GetPointerToByteStringW(By
Dim tmp() As Byte
Dim tmplen As Long
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp
End If
End If
End Function
'Just call the function GetServers
' and check now all servers are get listed
;-)
Shiju
Hi,
Not sure really, but you're not initiaslising lPreferedMaxLen, which, to ensure all are returned should be MAX_PREFERRED_LENGTH (-1). Have you checked the return values, and compared lEntriesRead to lTotalEntries? If your return value is ERROR_MORE_DATA then the buffer isn't large enough.
This link provides an example of NetServerEnum in VB from MSDN: http://support.microsoft.com/default.aspx?scid=kb;en-us;198896
Regards .. Alan
Not sure really, but you're not initiaslising lPreferedMaxLen, which, to ensure all are returned should be MAX_PREFERRED_LENGTH (-1). Have you checked the return values, and compared lEntriesRead to lTotalEntries? If your return value is ERROR_MORE_DATA then the buffer isn't large enough.
This link provides an example of NetServerEnum in VB from MSDN: http://support.microsoft.com/default.aspx?scid=kb;en-us;198896
Regards .. Alan
Try this
Set myobj = GetObject("WinNT://yourdom ainname")
myobj.Filter = Array("computer")
For Each comp In myobj
Debug.Print comp.Name
Next
Set myobj = GetObject("WinNT://yourdom
myobj.Filter = Array("computer")
For Each comp In myobj
Debug.Print comp.Name
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.