User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 403,587 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,137 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Sep 20th, 2004
Views: 6,652
Get computers on network
visualbasic Syntax | 5 stars
  1. '!!==========================
  2. ! PLACE THE FOLLOWING IN A FRM
  3. '!!==========================
  4.  
  5. Option Explicit
  6. '
  7.  
  8. Private Sub Form_Load()
  9. Call GetComputers(Me.List1)
  10. End Sub
  11.  
  12.  
  13.  
  14. '!!===========================
  15. '!! PLACE THE FOLLOWING IN A MOD
  16. '!!===========================
  17.  
  18. Option Explicit
  19.  
  20. Private Type SERVER_INFO_100
  21. sv100_platform_id As Long
  22. sv100_name As Long
  23. End Type
  24.  
  25. Private Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Long _
  26. , ByVal level As Long, buf As Any, ByVal prefmaxlen As Long _
  27. , entriesread As Long, totalentries As Long, ByVal servertype As Long _
  28. , ByVal domain As Long, resume_handle As Long) As Long
  29. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any _
  30. , uFrom As Any, ByVal lSize As Long)
  31. Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Long) As Long
  32. Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
  33.  
  34. Private Const MAX_PREFERRED_LENGTH As Long = -1
  35. Private Const SV_TYPE_WORKSTATION As Long = &H1
  36. Private Const NERR_SUCCESS As Long = 0&
  37. Private Const ERROR_MORE_DATA As Long = 234&
  38. '
  39.  
  40. Public Sub GetComputers(lst As ListBox)
  41. '===============================================================================
  42. Dim lngBufPtr As Long
  43. Dim dwEntriesRead As Long
  44. Dim dwTotalEntries As Long
  45. Dim dwResumeHandle As Long
  46. Dim se100 As SERVER_INFO_100
  47. Dim lngSuccess As Long
  48. Dim nStructSize As Long
  49. Dim lngCounter As Long
  50. Dim strComputer As String
  51. Dim blnNoComputers As Boolean
  52.  
  53. nStructSize = LenB(se100)
  54.  
  55. '-- Call passing MAX_PREFERRED_LENGTH to have the API allocate required memory for the
  56. ' return values.
  57.  
  58. '-- The call is enumerating all machines on the network (SV_TYPE_ALL); however, by
  59. ' Or'ing specific bit masks for defined types you can customize the returned data.
  60. ' For example, a value of 0x00000003 combines the bit masks for
  61. ' SV_TYPE_WORKSTATION (0x00000001) and SV_TYPE_SERVER (0x00000002).
  62.  
  63. '-- dwServerName must be Null. The level parameter (100 here) specifies the data
  64. ' structure being used (in this case a SERVER_INFO_100 structure).
  65.  
  66. '-- The domain member is passed as Null, indicating machines on the primary
  67. ' domain are to be retrieved. If you decide to use this member, pass
  68. ' StrPtr("domain name"), not the string itself.
  69.  
  70. lngSuccess = NetServerEnum(0&, 100, lngBufPtr, MAX_PREFERRED_LENGTH, dwEntriesRead _
  71. , dwTotalEntries, SV_TYPE_WORKSTATION, 0&, dwResumeHandle)
  72.  
  73. '-- If all goes well
  74. If lngSuccess = NERR_SUCCESS And lngSuccess <> ERROR_MORE_DATA Then
  75. lst.Clear
  76.  
  77. '-- Loop through the returned data, adding each machine to the list
  78. For lngCounter = 0 To dwEntriesRead - 1
  79. '-- Get one chunk of data and cast into an SERVER_INFO_100 struct
  80. ' in order to add the name to a list
  81. CopyMemory se100, ByVal lngBufPtr + (nStructSize * lngCounter), nStructSize
  82.  
  83. strComputer = GetPointerToByteStringW(se100.sv100_name)
  84.  
  85. lst.AddItem strComputer
  86. Next
  87.  
  88. blnNoComputers = (lst.ListCount = 0)
  89. End If
  90.  
  91. '-- Clean up regardless of lngSuccess
  92. Call NetApiBufferFree(lngBufPtr)
  93. End Sub
  94.  
  95. Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
  96. '===============================================================================
  97. Dim bytTemp() As Byte
  98. Dim lngTempLen As Long
  99.  
  100. If dwData <> 0 Then
  101. lngTempLen = lstrlenW(dwData) * 2
  102. If lngTempLen <> 0 Then
  103. ReDim bytTemp(0 To (lngTempLen - 1)) As Byte
  104. CopyMemory bytTemp(0), ByVal dwData, lngTempLen
  105. GetPointerToByteStringW = bytTemp
  106. End If
  107. End If
  108.  
  109. Erase bytTemp()
  110. End Function
Comments (Newest First)
minhnhat2807 | Newbie Poster | Dec 26th, 2007
Very long and difficult!
A easy way: You can use SQL Server to know avaible server on network!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC