I am an irritated person right now. I’m not kidding you, from about 8:30 till about 6:30 I was trying to figure out this horrific problem to a solution that was caused by an upgrade. I am a VB programmer of about 3 years (actual experience) and am moving towards VB.NET. I am still quite new to this programming language, but I thought, “What the heck. I am going to upgrade our old programs to VB.NET.” Well this was quite an arduous task and I’m only on the simplest program to begin with.
I found about 75 different solutions for finding all local workstations and servers that are online using API calls. The biggest problem with these solutions is that they were almost all written in either VB.NET pointing to the console or VB6, which did me absolutely no good whatsoever. I also didn’t need references to an Active Directory Connector since this program is used on a Peer-To-Peer network.
The following was the solution that I discovered that will allow for you to be able to upload to a list box all of the Workstations and Servers currently online on your domain or local network:
Step 1: The Form
Create a form: (The form name can be named anything)
(1) Listbox control:
Name: List1
(2) Button Controls:
Name: RefreshBTN
Name: QuitBTN
Insert the following code (replace necessary items such as form name or spelling and DO NOT OVERWRITE "Windows Form Designer generated code”):
[***BEGIN CODE***]
Option Strict Off
Option Explicit On
Friend Class ConnectFRM
Inherits System.Windows.Forms.Form
+["Windows Form Designer generated code…"]
Private Sub ConnectFRM_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Call GetServers()
End Sub
Private Sub QuitBTN_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles QuitBTN.Click
End
End Sub
Private Sub RefreshBTN_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles RefreshBTN.Click
List1.Items.Clear()
Call GetServers()
End Sub
Private Function GetServers() As Integer
'lists all servers of the specified type
'that are visible anywhere.
Dim bufptr As Integer
Dim nType As Integer = 100
Dim dwEntriesread As Integer
Dim dwTotalentries As Integer
Dim dwResumehandle As Integer
Dim se100 As SERVER_INFO_100
Dim success As Integer
Dim nStructSize As Integer
Dim cnt As Integer
nStructSize = Len(se100)
Try
'Verify that you can make a connection to the network
success = NetServerEnum(vbNullString, nType, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_ALL, vbNullString, dwResumehandle)
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, bufptr + (nStructSize * cnt), nStructSize)
'Add the new found item to the list of computers
List1.Items.Add(GetPointerToByteStringW(se100.sv100_name))
Next
End If
Catch ex As Exception
'Pending any errors (reguardless of shape or form) report the error
'Using a message box.
Dim ErrorMSG As String
ErrorMSG = "Error Source: " & ex.Source & Chr(13)
ErrorMSG = ErrorMSG & "Error Message: " & ex.Message & Chr(13)
ErrorMSG = ErrorMSG & "Process could not be completed"
MessageBox.Show(ErrorMSG, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'if all goes well
'clean up regardless of success
Call NetApiBufferFree(bufptr)
'return entries as sign of success
GetServers = dwEntriesread
End Try
End Function
Private Function GetPointerToByteStringW(ByVal dwData As Integer) As String
'Take the current pointer and convert to a string
Dim tmp() As Byte
Dim tmplen As Integer
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp((tmplen - 1))
CopyMemory(tmp(0), dwData, tmplen)
GetPointerToByteStringW = System.Text.UnicodeEncoding.Unicode.GetString(tmp)
End If
End If
End Function
End Class
[***END CODE***]
Step 2: The Module
Create a Module: (The module can be named anything)
Insert the following code (replace necessary items such as form name or spelling):
[***BEGIN CODE***]
Option Strict Off
Option Explicit On
Module Global
Public Const MAX_PREFERRED_LENGTH As Integer = -1
Public Const NERR_SUCCESS As Integer = 0
Public Const ERROR_MORE_DATA As Integer = 234
Public Const SV_TYPE_WORKSTATION As Integer = &H1S
Public Const SV_TYPE_SERVER As Integer = &H2S
Public Const SV_TYPE_SQLSERVER As Integer = &H4S
Public Const SV_TYPE_DOMAIN_CTRL As Integer = &H8S
Public Const SV_TYPE_DOMAIN_BAKCTRL As Integer = &H10S
Public Const SV_TYPE_TIME_SOURCE As Integer = &H20S
Public Const SV_TYPE_AFP As Integer = &H40S
Public Const SV_TYPE_NOVELL As Integer = &H80S
Public Const SV_TYPE_DOMAIN_MEMBER As Integer = &H100S
Public Const SV_TYPE_PRINTQ_SERVER As Integer = &H200S
Public Const SV_TYPE_DIALIN_SERVER As Integer = &H400S
Public Const SV_TYPE_XENIX_SERVER As Integer = &H800S
Public Const SV_TYPE_SERVER_UNIX As Integer = SV_TYPE_XENIX_SERVER
Public Const SV_TYPE_NT As Integer = &H1000S
Public Const SV_TYPE_WFW As Integer = &H2000S
Public Const SV_TYPE_SERVER_MFPN As Integer = &H4000S
Public Const SV_TYPE_SERVER_NT As Integer = &H8000S
Public Const SV_TYPE_POTENTIAL_BROWSER As Integer = &H10000
Public Const SV_TYPE_BACKUP_BROWSER As Integer = &H20000
Public Const SV_TYPE_MASTER_BROWSER As Integer = &H40000
Public Const SV_TYPE_DOMAIN_MASTER As Integer = &H80000
Public Const SV_TYPE_SERVER_OSF As Integer = &H100000
Public Const SV_TYPE_SERVER_VMS As Integer = &H200000
Public Const SV_TYPE_WINDOWS As Integer = &H400000 'Windows95 and above
Public Const SV_TYPE_DFS As Integer = &H800000 'Root of a DFS tree
Public Const SV_TYPE_CLUSTER_NT As Integer = &H1000000 'NT Cluster
Public Const SV_TYPE_TERMINALSERVER As Integer = &H2000000 'Terminal Server
Public Const SV_TYPE_DCE As Integer = &H10000000 'IBM DSS
Public Const SV_TYPE_ALTERNATE_XPORT As Integer = &H20000000 'rtn alternate transport
Public Const SV_TYPE_LOCAL_LIST_ONLY As Integer = &H40000000 'rtn local only
Public Const SV_TYPE_DOMAIN_ENUM As Integer = &H80000000
Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Public Const SV_PLATFORM_ID_OS2 As Integer = 400
Public Const SV_PLATFORM_ID_NT As Integer = 500
Public Const MAJOR_VERSION_MASK As Integer = &HFS
Public Structure SERVER_INFO_100
Dim sv100_platform_id As Integer
Dim sv100_name As Integer
End Structure
Public Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Integer, ByVal Level As Integer, ByRef buffer As Integer, ByRef prefmaxlen As Integer, ByRef entriesread As Integer, ByRef totalentries As Integer, ByVal servertype As Integer, ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer
Public Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Integer) As Integer
Public Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (ByRef pTo As SERVER_INFO_100, ByVal uFrom As Integer, ByVal lSize As Integer)
Public Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (ByRef pTo As Byte, ByVal uFrom As Integer, ByVal lSize As Integer)
Public Declare Function lstrlenW Lib "Kernel32" (ByVal lpString As Integer) As Integer
Public Declare Function GetComputerName Lib "Kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Public Declare Function GetLogicalDriveStrings Lib "Kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, ByVal lpBuffer As String) As Integer
Public Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Integer) As Integer
End Module
[***END CODE***]
Try and run the program to see if the desired results work.
Or you can download the demo from:
Please let me know how this works. If the link does not work, please email me and I will shoot you over my solution files.
darkstarjk@hotmail.com
I found about 75 different solutions for finding all local workstations and servers that are online using API calls. The biggest problem with these solutions is that they were almost all written in either VB.NET pointing to the console or VB6, which did me absolutely no good whatsoever. I also didn’t need references to an Active Directory Connector since this program is used on a Peer-To-Peer network.
The following was the solution that I discovered that will allow for you to be able to upload to a list box all of the Workstations and Servers currently online on your domain or local network:
Step 1: The Form
Create a form: (The form name can be named anything)
(1) Listbox control:
Name: List1
(2) Button Controls:
Name: RefreshBTN
Name: QuitBTN
Insert the following code (replace necessary items such as form name or spelling and DO NOT OVERWRITE "Windows Form Designer generated code”):
[***BEGIN CODE***]
Option Strict Off
Option Explicit On
Friend Class ConnectFRM
Inherits System.Windows.Forms.Form
+["Windows Form Designer generated code…"]
Private Sub ConnectFRM_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Call GetServers()
End Sub
Private Sub QuitBTN_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles QuitBTN.Click
End
End Sub
Private Sub RefreshBTN_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles RefreshBTN.Click
List1.Items.Clear()
Call GetServers()
End Sub
Private Function GetServers() As Integer
'lists all servers of the specified type
'that are visible anywhere.
Dim bufptr As Integer
Dim nType As Integer = 100
Dim dwEntriesread As Integer
Dim dwTotalentries As Integer
Dim dwResumehandle As Integer
Dim se100 As SERVER_INFO_100
Dim success As Integer
Dim nStructSize As Integer
Dim cnt As Integer
nStructSize = Len(se100)
Try
'Verify that you can make a connection to the network
success = NetServerEnum(vbNullString, nType, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_ALL, vbNullString, dwResumehandle)
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, bufptr + (nStructSize * cnt), nStructSize)
'Add the new found item to the list of computers
List1.Items.Add(GetPointerToByteStringW(se100.sv100_name))
Next
End If
Catch ex As Exception
'Pending any errors (reguardless of shape or form) report the error
'Using a message box.
Dim ErrorMSG As String
ErrorMSG = "Error Source: " & ex.Source & Chr(13)
ErrorMSG = ErrorMSG & "Error Message: " & ex.Message & Chr(13)
ErrorMSG = ErrorMSG & "Process could not be completed"
MessageBox.Show(ErrorMSG, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'if all goes well
'clean up regardless of success
Call NetApiBufferFree(bufptr)
'return entries as sign of success
GetServers = dwEntriesread
End Try
End Function
Private Function GetPointerToByteStringW(ByVal dwData As Integer) As String
'Take the current pointer and convert to a string
Dim tmp() As Byte
Dim tmplen As Integer
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp((tmplen - 1))
CopyMemory(tmp(0), dwData, tmplen)
GetPointerToByteStringW = System.Text.UnicodeEncoding.Unicode.GetString(tmp)
End If
End If
End Function
End Class
[***END CODE***]
Step 2: The Module
Create a Module: (The module can be named anything)
Insert the following code (replace necessary items such as form name or spelling):
[***BEGIN CODE***]
Option Strict Off
Option Explicit On
Module Global
Public Const MAX_PREFERRED_LENGTH As Integer = -1
Public Const NERR_SUCCESS As Integer = 0
Public Const ERROR_MORE_DATA As Integer = 234
Public Const SV_TYPE_WORKSTATION As Integer = &H1S
Public Const SV_TYPE_SERVER As Integer = &H2S
Public Const SV_TYPE_SQLSERVER As Integer = &H4S
Public Const SV_TYPE_DOMAIN_CTRL As Integer = &H8S
Public Const SV_TYPE_DOMAIN_BAKCTRL As Integer = &H10S
Public Const SV_TYPE_TIME_SOURCE As Integer = &H20S
Public Const SV_TYPE_AFP As Integer = &H40S
Public Const SV_TYPE_NOVELL As Integer = &H80S
Public Const SV_TYPE_DOMAIN_MEMBER As Integer = &H100S
Public Const SV_TYPE_PRINTQ_SERVER As Integer = &H200S
Public Const SV_TYPE_DIALIN_SERVER As Integer = &H400S
Public Const SV_TYPE_XENIX_SERVER As Integer = &H800S
Public Const SV_TYPE_SERVER_UNIX As Integer = SV_TYPE_XENIX_SERVER
Public Const SV_TYPE_NT As Integer = &H1000S
Public Const SV_TYPE_WFW As Integer = &H2000S
Public Const SV_TYPE_SERVER_MFPN As Integer = &H4000S
Public Const SV_TYPE_SERVER_NT As Integer = &H8000S
Public Const SV_TYPE_POTENTIAL_BROWSER As Integer = &H10000
Public Const SV_TYPE_BACKUP_BROWSER As Integer = &H20000
Public Const SV_TYPE_MASTER_BROWSER As Integer = &H40000
Public Const SV_TYPE_DOMAIN_MASTER As Integer = &H80000
Public Const SV_TYPE_SERVER_OSF As Integer = &H100000
Public Const SV_TYPE_SERVER_VMS As Integer = &H200000
Public Const SV_TYPE_WINDOWS As Integer = &H400000 'Windows95 and above
Public Const SV_TYPE_DFS As Integer = &H800000 'Root of a DFS tree
Public Const SV_TYPE_CLUSTER_NT As Integer = &H1000000 'NT Cluster
Public Const SV_TYPE_TERMINALSERVER As Integer = &H2000000 'Terminal Server
Public Const SV_TYPE_DCE As Integer = &H10000000 'IBM DSS
Public Const SV_TYPE_ALTERNATE_XPORT As Integer = &H20000000 'rtn alternate transport
Public Const SV_TYPE_LOCAL_LIST_ONLY As Integer = &H40000000 'rtn local only
Public Const SV_TYPE_DOMAIN_ENUM As Integer = &H80000000
Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Public Const SV_PLATFORM_ID_OS2 As Integer = 400
Public Const SV_PLATFORM_ID_NT As Integer = 500
Public Const MAJOR_VERSION_MASK As Integer = &HFS
Public Structure SERVER_INFO_100
Dim sv100_platform_id As Integer
Dim sv100_name As Integer
End Structure
Public Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Integer, ByVal Level As Integer, ByRef buffer As Integer, ByRef prefmaxlen As Integer, ByRef entriesread As Integer, ByRef totalentries As Integer, ByVal servertype As Integer, ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer
Public Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Integer) As Integer
Public Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (ByRef pTo As SERVER_INFO_100, ByVal uFrom As Integer, ByVal lSize As Integer)
Public Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (ByRef pTo As Byte, ByVal uFrom As Integer, ByVal lSize As Integer)
Public Declare Function lstrlenW Lib "Kernel32" (ByVal lpString As Integer) As Integer
Public Declare Function GetComputerName Lib "Kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Public Declare Function GetLogicalDriveStrings Lib "Kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, ByVal lpBuffer As String) As Integer
Public Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Integer) As Integer
End Module
[***END CODE***]
Try and run the program to see if the desired results work.
Or you can download the demo from:
Please let me know how this works. If the link does not work, please email me and I will shoot you over my solution files.
darkstarjk@hotmail.com