Hey, all. I'm trying to write a program that can 'peek' at the network neighborhood and tell me if a certain computer (its NetBIOS name, I mean) is showing up on the network. In other words, is the thing up and running?
I've thought of two methods. One is to do what I said -- scan the list periodically, refreshing as necessary.
The other is to map a drive to that computer's hard drive, and just see if the program can access the drive.
Which would be easier? I have two considerations:
The computer that would be scanning is on a different network than the one it would be looking for. That is, this computer is a 192.* computer, the other is a 10.*. That's why I think it might be easier to map a drive...
If I map a drive, though, how do I get it to not time out and crash this computer in case it can't access it?
Couldn't you ping the computer since you know the ip address?
You can have allot of code to do ping in vb or you could
shellexecute the cmd.exe with the parameters already filled in
and pipe the results out to a text file. Then read the text file and
see the results.
VB Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
The PathIsNetworkPath API only does a string character eveluation
of the path provided. It does not check for the validity of the
resource. Test it out. This line from the example at allapi.net will
confirm it to you. When testing \\Server\C$... it comes up as
true for a network path even though my computer is not
named "Server". All it looks for is the double backslashes.
VB Code:
Me.Print "Is the specified path a network path? " + CStr(CBool(PathIsNetworkPath("\\Server\C$\test.txt")))
Sorry but it doesnt work for what the poster wants.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
No, since they're on two networks without a bridge. I can't ping between the two networks despite the fact I can share files and folders...
Originally posted by RobDog888 Couldn't you ping the computer since you know the ip address?
You can have allot of code to do ping in vb or you could
shellexecute the cmd.exe with the parameters already filled in
and pipe the results out to a text file. Then read the text file and
see the results.
VB Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
This code will simulate Network Neighborhood using two combos.
One for the Active Domains and another for the Active Computers
in the selected Domain combo. Maybe it will help you get closer to
a solution.
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 MAX_PREFERRED_LENGTH As Long = -1
Public Const NERR_SUCCESS As Long = 0&
Public Const ERROR_REQ_NOT_ACCEP = 71&
Public Const ERROR_MORE_DATA As Long = 234&
Public Const NERR_BASE = 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()
This code will simulate Network Neighborhood using two combos.
One for the Active Domains and another for the Active Computers
in the selected Domain combo. Maybe it will help you get closer to
a solution.
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 MAX_PREFERRED_LENGTH As Long = -1
Public Const NERR_SUCCESS As Long = 0&
Public Const ERROR_REQ_NOT_ACCEP = 71&
Public Const ERROR_MORE_DATA As Long = 234&
Public Const NERR_BASE = 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()
Basically you need a result back from a NetBIOS computer by name that tells you if it is online and its Server service is running I guess. There are lots of ways to go about this, but I don't see an obvious best choice.
One is to ask the machine what Domain/Workgroup it belongs to (if any). A non-reply should tell you what you need to know I suppose.
Code:
Option Explicit
Private Const NERR_SUCCESS As Long = 0
Public Enum NETSETUP_JOIN_STATUS
NetSetupUnknownStatus = 0
NetSetupUnjoined
NetSetupWorkgroupName
NetSetupDomainName
End Enum
Private Declare Function NetApiBufferFree Lib "netapi32" ( _
ByVal lpBuffer As Long) As Long
Private Declare Function NetGetJoinInformation Lib "netapi32" ( _
ByRef lpServer As Byte, _
ByRef lpNameBuffer As Long, _
ByRef BufferType As NETSETUP_JOIN_STATUS) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal pTo As Long, _
ByVal uFrom As Long, _
ByVal lSize As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" ( _
ByVal lpString As Long) As Long
Public Function GetJoinInfo( _
ByVal Server As String, _
ByRef Domain As String, _
ByRef BufferType As NETSETUP_JOIN_STATUS) As Boolean
'Find Domain or Workgroup name of Server. Empty Server means "self."
'Returns True on failure.
Dim bytServer() As Byte
Dim lngBufPtr As Long
Dim lngLen As Long
bytServer = Trim$(Server) & vbNullChar
If NetGetJoinInformation(bytServer(0), lngBufPtr, BufferType) = NERR_SUCCESS Then
lngLen = lstrlen(lngBufPtr)
Domain = String$(lngLen, 0)
CopyMemory StrPtr(Domain), lngBufPtr, lngLen * 2
NetApiBufferFree lngBufPtr
Else
GetJoinInfo = True
End If
End Function
You'll probably only care about the reply status (or error result) and not the returned "domain" name.
I am on workgroup with three computers(currently) connected via the five pin switch. I need to know how many computers are connected to my network at runtime using vb..... Guys thanks for your reply......
Its probably because its looking for a Domain and being in a Workgroup isnt the same thing. You can add error handling for the domain search part like so...
Code:
Option Explicit
'BEHIND A FORM WITH TWO COMBOS (cboDomain AND cboComputer)
Private Sub Form_Load()
On Error GoTo MyError
Dim compNames() As String
Dim x As Integer
'SETUP THE DOMAIN COMBO BOX
compNames = GetServers(vbNullString, SV_TYPE_DOMAIN_ENUM)
If UBound(compNames) >= 0 Then
For x = LBound(compNames) To UBound(compNames) - 1
cboDomain.AddItem compNames(x)
Next
cboDomain.ListIndex = 0
End If
Exit Sub
MyError:
If Err.Number = 9 Then
MsgBox "No Domains Found", vbOKOnly + vbExclamation, "Error"
Else
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, "Error"
End If
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Here is the cause of teh 6118 error message value...
The NetServerEnum function depends on the browser service being installed and running. If no browser servers are found, then NetServerEnum fails with ERROR_NO_BROWSER_SERVERS_FOUND.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
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 x As Integer
cboComputer.Clear
x = 0
compNames = GetServers(IIf(cboDomain.Text = vbNullString, vbNull, 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
If cboDomain.ListCount > 0 Then
cboDomain.RemoveItem (cboDomain.ListIndex)
cboDomain.ListIndex = 0
End If
compNames = GetServers(cboDomain.Text, SV_TYPE_ALL)
Resume
Else
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, App.ProductName
End If
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Another way to get the Network Neighborhood for the current Workgroup/Domain is:
Code:
Option Explicit
'Ref to: Microsoft Shell Controls and Automation.
Private Sub ListHosts(ByVal TextBox As TextBox)
Dim Shell As Shell32.Shell
Dim fldNetHood As Shell32.Folder
Dim itmHost As Shell32.FolderItem
Set Shell = New Shell32.Shell
With TextBox
.Text = "Retrieving Network Neighborhood..."
.Refresh
Set fldNetHood = Shell.NameSpace(ssfNETWORK)
.Text = ""
For Each itmHost In fldNetHood.Items
.SelStart = Len(.Text)
.SelText = itmHost.Name & vbNewLine
Next
End With
End Sub
Private Sub Form_Load()
Show
ListHosts Text1
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
Text1.Move 0, 0, ScaleWidth, ScaleHeight
End If
End Sub
This might be simpler for anyone just interested in machines on a home LAN, which are typically in the same Workgroup.