check if machine exists in network?

Posted on 2004-10-23
Last Modified: 2007-12-19
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.
Question by:aslandk
    LVL 6

    Expert Comment

    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.

    Author Comment

    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 ....?
    LVL 24

    Expert Comment

    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
    Dim Ret As QOCINFO
      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

    Expert Comment

    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
    LVL 24

    Expert Comment

    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
    LVL 6

    Expert Comment

    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).
    LVL 1

    Expert Comment

    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

    Expert Comment

    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 -
    Component Space - .NET Internet component suite -
    OstroSoft - ICMP Component -

    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!
    LVL 24

    Expert Comment

      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.

    Accepted Solution

    PAQed with no points refunded (of 500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 50
    scoresAverage challenge 8 42
    sumDigits  challenge 7 22
    allStar challenge 1 12
    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now