Solved

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

Posted on 2007-12-04
9
237 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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 9

Expert Comment

by:sognoct
Comment Utility
 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
Comment Utility
thank you sognoct
it must put in the form load?
0
 
LVL 9

Expert Comment

by:sognoct
Comment Utility
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
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
Comment Utility
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 120 total points
Comment Utility
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
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
SHOULD WE USE THIS IN A EXCEL AS A MACRO?

IS there anything we need to change?
0

Featured Post

Free Trending Threat Insights Every Day

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.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

11 Experts available now in Live!

Get 1:1 Help Now