Solved

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

Posted on 2007-12-04
9
238 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
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
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20401915
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 20402282
0
 
LVL 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now