How do get a list of all the pc's connected on my network and list them in a list box. its so i can send a message to the client app and tell it to shutdown, restart, log off etc.
Printable View
How do get a list of all the pc's connected on my network and list them in a list box. its so i can send a message to the client app and tell it to shutdown, restart, log off etc.
doesnt anybody know how to do this?
you can get all names of computer which are on network using winsock control and send them message too.
This will simulate network neighborhood using two combos.
Usage:VB Code:
'IN A MODULE Option Explicit Public Type SERVER_INFO_100 sv100_platform_id As Long sv100_name As Long End Type Public Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000 Public Const SV_TYPE_ALL As Long = &HFFFFFFFF Public Const SV_TYPE_SQLSERVER As Long = &H4 Public Const MAX_PREFERRED_LENGTH As Long = -1 Public Const NERR_SUCCESS As Long = 0& Public Const ERROR_REQ_NOT_ACCEP As Long = 71& Public Const ERROR_MORE_DATA As Long = 234& Public Const NERR_BASE As Long = 2100 Public Const NERR_InvalidComputer = (NERR_BASE + 251) Public 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 Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long) Public Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Long) As Long Public Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long Public Function GetServers(sDomain As String, sType As Long) As String() 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, sType, StrPtr(sDomain), dwResumehandle) 'success = 71 'NO ACTIVE 'DOMAIN' If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then Dim compNames() As String ReDim compNames(dwEntriesread) For Cnt = 0 To dwEntriesread - 1 CopyMemory se100, ByVal bufptr + (nStructSize * Cnt), nStructSize compNames(Cnt) = GetPointerToByteStringW(se100.sv100_name) DoEvents Next End If Call NetApiBufferFree(bufptr) GetServers = compNames End Function Public Function GetPointerToByteStringW(ByVal 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
:thumb:VB Code:
'BEHIND A FORM WITH TWO COMBO'S (cboDomain and cboComputer) Private Sub Form_Load() Dim compNames() As String 'SETUP THE DOMAIN COMBO BOX compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM) For x = LBound(compNames) To UBound(compNames) - 1 cboDomain.AddItem compNames(x) Next cboDomain.ListIndex = 0 End Sub Private Sub cboDomain_Click() 'NEED TO CLEAR CBOCOMPUTER AND RELOAD WITH COMPUTERS IN THE NEWLY SELECTED DOMAIN 'SETUP THE COMPUTER COMBO BOX On Error GoTo No_Bugs Dim compNames() As String Dim compName As String Dim x As Integer cboComputer.Clear x = 0 compNames = GetServers(cboDomain.Text, SV_TYPE_ALL) If UBound(compNames) > 0 Then For x = LBound(compNames) To UBound(compNames) - 1 cboComputer.AddItem compNames(x) Next cboComputer.ListIndex = 0 End If Exit Sub No_Bugs: If Err.Number = "9" Then cboDomain.RemoveItem (cboDomain.ListIndex) cboDomain.ListIndex = 0 compNames = GetServers(cboDomain.Text, SV_TYPE_ALL) Resume Else MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, App.ProductName End If End Sub
Hey Rob. If I remove all computers from the network, i get an error and go into debug mode and is highlit like this....is this ok?
----------------------------------
Private Sub Form_Load()
Dim compNames() As String
'SETUP THE DOMAIN COMBO BOX
compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM)
For x = LBound(compNames) To UBound(compNames) - 1
cboDomain.AddItem compNames(x)
Next
cboDomain.ListIndex = 0
End Sub
No, then your not detecting any domains/workgroups at all so the for statement is for x = 0 to 0 - 1 which gives an error. You can trap for when you get nothing with an If block surrounding the for loop.
Did you unplug your cable :DVB Code:
If LBound(compNames) = 0 And LBound(compNames) = 0 Then MsgBox "No Domains/Workgroups Detected" Else 'Do all the for loop stuff in here End If
do u mean the coding like this:
'BEHIND A FORM WITH TWO COMBO'S (cboDomain and cboComputer)
Private Sub Form_Load()
Dim compNames() As String
'SETUP THE DOMAIN COMBO BOX
compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM)
If LBound(compNames) = 0 And LBound(compNames) = 0 Then
MsgBox "No Domains/Workgroups Detected"
Else
For x = LBound(compNames) To UBound(compNames) - 1
cboDomain.AddItem compNames(x)
Next
cboDomain.ListIndex = 0
End If
End Sub
I would Clear the listbox first, and post the code in the Form_Activate instead.
You may want to call it again.
Yes like that. No need to clear the combo box if this is on the Form_Load event unless you added items
to it at design time.
hmmm now when i run it i get another error: "Subscript out of range"
and this is highlit
--------------------------------
'BEHIND A FORM WITH TWO COMBO'S (cboDomain and cboComputer)
Private Sub Form_Load()
Dim compNames() As String
'SETUP THE DOMAIN COMBO BOX
compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM)
If LBound(compNames) = 0 And LBound(compNames) = 0 Then
Did you add the other code to a module? Are you connected to a network? Try stepping through the code to see how far you get.
No i didnt add the code to a module, i added it to the right bit. LOL no im not at the moment, but that wont be fixed until friday. Thanks for the help tho, its been great! :D
Ok, if you still have any issues post back tomorrow. :thumb:
Try to go to this Site. It may help you.Quote:
Originally Posted by boku
He needs to be connected to a network or workgroup first ;)Quote:
Originally Posted by Simply Me
Hi there RobDog888, will you code work on all Windows OS? How about a network with different Workgroups? I've been searching for quite awhile now for something like your code does, though I havent tried it yet as of this moment.