• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

check if machine exists in network?

I have made a little applikation which checks some settings on a range of machien in the network. It works fine, but if a machine i unavailable(turned off or machien name is unvalid), then the appliaktion teminates with:

"The remote machine does not exist or is unavailble"....and then my applikation stop running.

I would like the applikation to proced to the next machine if a machine is not to be reached.

Can somebody help me??
The applikation is made in VB, but a vbs scritpt is also ok.
1 Solution
Very hard to answer without seeing your code, but locate the line that causes the crash (probably the one that opens the remote connection) and add some error handling around it. Something like the following might do it:

On Error Goto NextComputer
  ' Check settings here


On Error Goto 0
' Loop or whatever you do to check the next computer.
aslandkAuthor Commented:
The error occures in this linie:
Set UserSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & computername & "").InstancesOf("Win32_ComputerSystem")

What is the best to catch that kind of error? Is it "just" On Error Goto ....?
Declare this:

Private Type QOCINFO
    dwSize As Long
    dwFlags As Long
    dwInSpeed As Long 'in bytes/second
    dwOutSpeed As Long 'in bytes/second
End Type
Private Declare Function IsDestinationReachable Lib "SENSAPI.DLL" Alias "IsDestinationReachableA" (ByVal lpszDestination As String, ByRef lpQOCInfo As QOCINFO) As Long

' Then in your Sub/Function declare
  Ret.dwSize = Len(Ret)

'Then before your Set UserSet
  ' Perform a loop here changing computername as you do now
  If IsDestinationReachable(computername, Ret) = 0 Then
      ' Your Code Here
      ' Log Error Here or just ignore
  End If
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

You havn't another way to catch this error, you can ony handle it and proceed to the next machine

On Error GoTo Error_Handle

'Your Loop

Set UserSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & computername & "").InstancesOf("Win32_ComputerSystem")

'End Your Loop

    Select Case Err.Number
       Case 462:
       'Here you can handle "The remote machine does not exist or is unavailble"
       'and return to you loop
       'Other errors will not be handled
       Debug.Print Err.Description
       Resume Next
    End Select
Whoops mistyped Change
  If IsDestinationReachable(computername, Ret) = 0 Then
      ' Your Code Here
      ' Log Error Here or just ignore
  End If


  If IsDestinationReachable(computername, Ret) = 0 Then
      ' Log Error Here or just ignore
      ' Your code here
  End If
VB isn't packed with sophisticated error handling methods, so On Error Goto is pretty much your only way. The "IsDestinationReachable" approach is works too, but causes an unnecessary connection being opened - and you need to check for errors anyway if you want your code to be really robust (getting the information can fail in other ways too).
Do you know what port you are using to connect to the pc?
If you do, you can use a winsock control to see if the pc is there

    Winsock1.RemoteHost = theIpAddress
    Winsock1.RemotePort = thePortNumber

'have connected
Private Sub Winsock1_Connect()
  'continue your code
End Sub

'Not connected
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
   'goto the next computer
End Sub
On Error statements in Visual Basic are available and can be utilized and called in a variety of methods to suit your needs. The methods implemented derive from `C` programming, and have been adapted into the Visual Basic syntax.

' Suppress Error notification with Visual Basic.
On Error Resume Next

Other available methods are also easily implemented in much the same way, these being.

-: On Error GoTo 0
-: On Error Resume Next
-: On Error GoTo line

A standard practice when tracing errors in applications is to implement a basic routine which catches and displays return code values for each routine independently.

‘ Define reference to our error handler
On Error GoTo err_handler:

‘ Code functions within subroutine go here:

‘ We expect 1object numeric and1object as text
  txtOutput.Text = "Error " & Err.Number & ": " & Err.Description

End Sub

We can elaborate on this method and build up a stack with variation in formatting as demonstrated:

On Error GoTo Error1
    Exit Sub

    On Error GoTo Error2
    MsgBox "Error1:" & Str$(Err.Number) & "." & vbCrLf & _
    Resume Next

    MsgBox "Error2:" & Str$(Err.Number) & "." & vbCrLf & _
    Resume Next

We can use a bespoke handler in our application as errors generate, as we know what to expect from each error and can determine how to handle each independently. Illustrated in the case of your routine not being available to demonstrate how you are utilizing Active-X components within your application or if you are making native systems calls with VB or VB Script, I have chose to illustrate with a precompiled Active-X component.

Visual Basic developers have an array of 3rd party Active-X components and `dynamic linked libraries` - DLL extensions which makes for rapid application development and cuts development to the work of building custom GUI’s in VB or C#. As a result these components are common purchases in SME environments.

When redistribution and commercial deployment of the application is a real consideration, it is recommended investing in creating your own DLL to provide the `Network Layer` and API required for your VB front end.
Some of the unique or distinctive products, providing pre-defined methods for `out of the box` network programmings are:

Catalyst – Socket Tools - http://www.catalyst.com/
Component Space - .NET Internet component suite - http://www.componentspace.com/icmp.aspx
OstroSoft - ICMP Component - http://www.ostrosoft.com/ICMP_component.asp

We need a routine which sweeps an IP range belonging to a specific subnet. For the purpose of this routine I have chosen to use the TCP command ICMP_NETECHO which is provided by the OstroSoft `ICMP Component` which I have downloaded and unzipped the “OSICMP.dll” from the archive to my system directory, and registered with the following command:

C:\WINDOWS>regsvr32 OSICMP.dll

When looking into the guide on and example code you can see an implementation of the error handling discussed previously and out of the box an example application is ready to compile and adapt to your needs with little effort.

Happy Programming!
  aslandk has not left the building just this question,(s)he just asked another new question yesterday in VB. There are a couple of workable solutions posted, at the least PAQ no refund.
PAQed with no points refunded (of 500)

Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now