Solved

how can  i make sure if the pc is connect or not  the net?

Posted on 2007-12-04
9
241 Views
Last Modified: 2010-04-30
Hello Eperts,
i need a code that can check if the pc is connected to the net or not.
the program should do some instructions after that the connection is estanbilished.


0
Comment
Question by:aitsu01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 20401841
 Function isConnected() As Boolean
    If My.Computer.Network.IsAvailable = True Then
      MsgBox("Computer is connected.")
    Else
      MsgBox("Computer is not connected.")
    End If
  End Function
0
 

Author Comment

by:aitsu01
ID: 20401859
thank you sognoct
it must put in the form load?
0
 
LVL 9

Expert Comment

by:sognoct
ID: 20401909
You can put this everywhere you need ...
if you want to get a boolean state change it in

 Function isConnected() As Boolean
    If My.Computer.Network.IsAvailable = True Then
      return true
    Else
       return false
    End If
  End Function

then you can write

if isConnected() then   MsgBox("Computer is connected.")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20401915
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 20402282
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 120 total points
ID: 20402331
BTW, detecting if a network is available does not garantee that the Internet is available. better use a Ping: http://www.andreavb.com/forum/viewtopic_2780.html
0
 
LVL 11

Expert Comment

by:bsharath
ID: 20403056
aitsu01
Can you post inb the solution here so it would be useful for others and if the linked sites expires the info would be here.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20403323
as requested by bsharath:

Throw the following code into a module or class (or form if you change the constants to private), then simply use:

Dim Result As Integer

Result = Ping("www.google.com")

If Ping < 0 Then MsgBox "Ping failed, error: " & Result

A reply of -1 means the ping timed out, -2 indicates any other error.

Default timeout to wait for a reply is 2 seconds (2000 ms) but this can be overridden.

Option Explicit

Const SOCKET_ERROR = 0
Const MAX_IP = 10

Private Type WSAdata
    wVersion As Integer
    wHighVersion As Integer
    szDescription(0 To 255) As Byte
    szSystemStatus(0 To 128) As Byte
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpVendorInfo As Long
End Type

Private Type Hostent
    h_name As Long
    h_aliases As Long
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long
End Type

Private Type IP_OPTION_INFORMATION
    TTL As Byte
    Tos As Byte
    Flags As Byte
    OptionsSize As Long
    OptionsData As String * 128
End Type

Private Type IP_ECHO_REPLY
    Address(0 To 3) As Byte
    Status As Long
    RoundTripTime As Long
    DataSize As Integer
    Reserved As Integer
    data As Long
    Options As IP_OPTION_INFORMATION
End Type

Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean

Public Function Ping(sAddr As String, Optional Timeout As Integer = 2000) As Integer
Dim hFile As Long, lpWSAdata As WSAdata
Dim hHostent As Hostent, AddrList As Long
Dim Address As Long, rIP As String
Dim OptInfo As IP_OPTION_INFORMATION
Dim EchoReply As IP_ECHO_REPLY

Call WSAStartup(&H101, lpWSAdata)

If GetHostByName(sAddr + String(64 - Len(sAddr), 0)) <> SOCKET_ERROR Then
    CopyMemory hHostent.h_name, ByVal GetHostByName(sAddr + String(64 - Len(sAddr), 0)), Len(hHostent)
    CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
    CopyMemory Address, ByVal AddrList, 4
End If

hFile = IcmpCreateFile()

If hFile = 0 Then
    Ping = -2 ' MsgBox "Unable to Create File Handle"
    Exit Function
End If

OptInfo.TTL = 255

If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, Timeout) Then
    rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
Else
    Ping = -1 ' MsgBox "Timeout"
End If

If EchoReply.Status = 0 Then
    Ping = EchoReply.RoundTripTime
Else
    Ping = -3
End If

IcmpCloseHandle hFile
WSACleanup

End Function
 
0
 
LVL 11

Expert Comment

by:bsharath
ID: 20403398
SHOULD WE USE THIS IN A EXCEL AS A MACRO?

IS there anything we need to change?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

695 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