Jump to content

Recommended Posts

Posted (edited)

This is a method to test connectivity to the Internet using the same approach as Mircosoft does, therefore no need to contact third party sites such as google or bing, which has often been the case.

Function:

; #FUNCTION# ====================================================================================================================
; Name ..........: _MicrosoftInternetConnectivity
; Description ...: Test if the local machine is connected to the Internet using the same method as Microsoft for 'network awareness' minus the dns records verification.
; Syntax ........: _MicrosoftInternetConnectivity()
; Parameters ....: None
; Return values .: Success: True
;                  Failure: False
; Author ........: guinness
; Remarks .......: This will contact the website www.msftncsi.com. Works with Windows 2000+
;                  Requires TCPStartup() to be initialised.
;                  The function has a restriction of contacting Microsoft every 15 seconds, so if you call the function within a tight loop then the last response will
;                  be returned and @extended set to non-zero.
; Link ..........: http://technet.microsoft.com/en-us/library/cc766017(v=ws.10).aspx#BKMK_How
; Example .......: Yes
; ===============================================================================================================================
Func _MicrosoftInternetConnectivity()
    Local Static $hTimer = 0, _ ; Create a static variable to store the timer handle.
            $bLastResponse = Null ; Create a static variable to store the last response.
    If TimerDiff($hTimer) < 15000 And Not ($bLastResponse = Null) Then ; If still in the timer and $bLastResponse contains a value.
        Return SetExtended(1, $bLastResponse) ; Return the last response instead and set @extended to 1.
    EndIf

    Local $bEnableActiveProbing = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'EnableActiveProbing') > 0
    If @error Then
        $bEnableActiveProbing = True
    EndIf
    If Not $bEnableActiveProbing Then
        Return False
    EndIf
    Local $sContent = 'Microsoft NCSI', _
            $sDNSHost = 'dns.msftncsi.com', _
            $sDNSIPAddress = '131.107.255.255', _
            $sURL = 'http://' & RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'ActiveWebProbeHost') & '/' & RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'ActiveWebProbePath')
    If $sURL == 'http:///' Then
        $sURL = 'http://www.msftncsi.com/ncsi.txt'
    Else
        $sContent = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'ActiveWebProbeContent')
        $sDNSIPAddress = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'ActiveDnsProbeContent')
        $sDNSHost = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet\', 'ActiveDnsProbeHost')
    EndIf

    $bLastResponse = BinaryToString(InetRead($sURL, $INET_FORCERELOAD)) == $sContent And TCPNameToIP($sDNSHost) == $sDNSIPAddress
    Return $bLastResponse
EndFunc   ;==>_MicrosoftInternetConnectivity

Example use of Function:


#include <InetConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    TCPStartup()
    Local $bConnected = _MicrosoftInternetConnectivity()
    MsgBox($MB_SYSTEMMODAL, '', 'Is the Internet working?: ' & ($bConnected ? 'Yes, the Internet is working.' : 'No, the Internet isn''t working.'))
    TCPShutdown()
EndFunc   ;==>Example
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Is there a difference?

Func _MicrosoftInternetConnectivity()
    Local Const $INET_FORCERELOAD = 1
    If BinaryToString(InetRead('http://www.msftncsi.com/ncsi.txt', $INET_FORCERELOAD)) == 'Microsoft NCSI' Then Return 1
EndFunc   ;==>_MicrosoftInternetConnectivity


#include <Misc.au3>

Local $fConnected = _MicrosoftInternetConnectivity()
MsgBox(4096, 'Is the Internet working?: ', _Iif($fConnected, 'Yes, the Internet is working.', 'No, the Internet isn''t working.')

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted

  On 1/7/2013 at 10:53 PM, 'JohnOne said:

Is there a difference?

Yes, because in Vista+ you can set these entries in the registry to your own server, data file and key text, so instead of using static results, I opted to use those set in the registry by users or sys admins alike.

Before you ask, I had to use http://www.msftncsi.com/ncsi.txt for those who still use Windows XP.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I read a load of stuff that led me to the same link as you posted a while back and after

making function similar to yours, I could not see the point, and just used the broke down version.

Then thought, hmm, well it's still contacting an external site, might as well just use my own func

with a number of different sites.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted

Neither approach is wrong, just some users may feel uncomfortable contacting Microsoft for Internet connectivity and would rather use their own server. Also, as you've selected 'Agree' to Microsoft's TOS, then you've already consented that you're happy to contact the Microsoft servers, for updates, time synchronisation and/or network awareness (which can be voluntarily disabled.)

If someone is contacting third party sites for Internet connectivity without prior consent from the user, the I would re-consider/think your approach in doing so, as it may land you in trouble*.

* - This is speculation and may depend on the site you're using. I'm also not a lawyer.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/8/2013 at 12:10 AM, 'JohnOne said:

Indeed.

You have made a few GetIP funcs as I recall, might be worth adding this site to it.

I wish I could, but this function is unable to retrieve the IP address.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/7/2013 at 11:22 PM, 'guinness said:

If someone is contacting third party sites for Internet connectivity without prior consent from the user, the I would re-consider/think your approach in doing so, as it may land you in trouble.

So i can get in trouble if i check connectivity by pinging some sites for example?

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

  On 1/8/2013 at 12:45 PM, 'careca said:

So i can get in trouble if i check connectivity by pinging some sites for example?

As I said I'm not a lawyer and it could/can depend on many deciding factors such as the 'Terms of Service' for the particular website you're pinging.

As a developer you should look at it from not only the developer's perspective but the end user's too. Think of it like this, would you be happy if you found out that a developer was contacting the likes of google.com/bing.com/yahoo.com, who in turn were building a profile about your access to their site (IP address, user-agent of application etc.) and even better without your consent. Now I have absolute zero problems with those websites, but in terms of morals you should at least let the end user know.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I get your point.

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

  • 3 weeks later...
Posted (edited)

Now this function resolves whether the PC is connected to the Internet using the same process as Microsoft. It will request data from http://www.msftncsi.com/ncsi.txt and resolve the IP address of dns.msftncsi.com. Of course these values can be changed via the registry if you want to use your own server.

Note: Before it wasn't resolving the IP address.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I forgot to take into consideration that this should work on XP, so when I added resolving the dns address, I didn't take into account the registry keys would return blank strings.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 year later...
Posted

Updated the example and function to meet the standards of AutoIt 3.3.10.0+.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

@guinness: Why update all scripts to use the ternary operator? Just leave them as they are and the examples work on both 3.3.8.1 and 3.3.10.2. You force everyone to update to latest version, but not everyone wants this and I don't see a benefit in updating already running examples. If they do not work with latest beta, then it's OK, but not if they run on both versions.

;)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Posted (edited)

  On 3/1/2014 at 2:11 PM, funkey said:

@guinness: Why update all scripts to use the ternary operator?

Because I like writing cleaner code and it's just an example. It doesn't take a lot for someone to change it to work in an older (buggier) version of AutoIt! Also it's at my discretion whether or not I want to update my code snippets for the benefit of the community.

I was hoping it was a "thanks guinnness!" But wishful thinking as usual.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 month later...
Posted (edited)

It's your choice guinness, but do remember alot of people still use windows 2000 or even Windows NT 4.0, especially in factory or manufacturing plant, changing an OS means changing the entire machinery. The cost is huge and it isnt cost effective at all.

Edited by CyberMax
Posted

I am not going to start writing code for an AutoIt version that came out 6 years ago.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 month later...
Posted

I added a 5 second timer so as not to flood Microsoft with hundreds of calls.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Interesting that it is only now that I've found.
Thanks for sharing.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

It's basically how _WinAPI_IsInternetConnected() works.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...