Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-12-04
9
Medium Priority
?
244 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 360 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

610 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