Jump to content

Recommended Posts

Posted (edited)

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinINet_InternetCheckConnection
; Description ...: Allows an application to check if a connection to the Internet can be established.
; Syntax ........: _WinINet_InternetGetConnectedStateEx(lpszUrl,dwFlags, dwReserved)
; Parameters ....:
;~               lpszUrl -Pointer to a null-terminated string that specifies the URL to use to check the connection. This value can be NULL.
;~               dwFlags FLAG_ICC_FORCE_CONNECTION is the only flag that is currently available. If this flag is set, it forces a connection. A sockets connection is attempted in the following order:
;~               wReserved -   This parameter is reserved and must be 0.
; Remarks
;                   * If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host.
;                   * If lpszUrl is NULL and there is an entry in the internal server database for the nearest server, the host value is extracted from the entry and used to ping that server.
; Return Value: Returns TRUE if a connection is made successfully, or FALSE otherwise. Use GetLastError to retrieve the error code.
;                   ERROR_NOT_CONNECTED is returned by GetLastError if a connection cannot be made or if the sockets database is unconditionally offline
;
; Author ........: Rajesh V R (thanks to SmOke_N for making me understand the dllcall better)
; Release Date...: 12 Apr 2009
; Related .......: _WinINet_InternetGetConnectedState
; Link ..........: @@MsdnLink@@ InternetCheckConnection
; Example .......:
; ===============================================================================================================================
Const $DW_FLAG_ICC_FORCE_CONNECTION = 0x00000001

Func _WinINet_InternetCheckConnection($lpsz_URL, $dw_Flags = $DW_FLAG_ICC_FORCE_CONNECTION, $w_Reserved = 0)
    
    ; Make DLL call
    Local $varResult = DllCall("wininet.dll", "int", "InternetCheckConnection", "str", $lpsz_URL, "uint", $dw_Flags, "uint", $w_Reserved)
    ; Return response
    If @error Then Return SetError(-1, 0, "")
    Return SetError($varResult[0], $varResult[1], $varResult[0])

EndFunc   ;==>_WinINet_InternetCheckConnection

#Region ; example


Local $message = ""

$message &= ".........................................." & @CRLF
$message &= "Site:" & @TAB & "http://www.google.com " & @CRLF & "Result:" &  @TAB & _WinINet_InternetCheckConnection("http://www.google.com") & @CRLF
$message &= "Site:" & @TAB & "testing.invalid.url   " & @CRLF & "Result:" & @TAB & _WinINet_InternetCheckConnection("testing.invalid.url") & @CRLF
$message &= ".........................................."

MsgBox(0, "_WinINet_InternetCheckConnection", $message)

#EndRegion

Thanks to AutoIT Team first

  • Expected bugfixes (help from advanced users of course is needed)
  • Doesnt work for "" value as indicated in documentation. - my bad, miscomprehended information...
  • cant get to work for some valid urls like smtp.google.com, etc (almost all those which dont normally respond for ping - possible restriction of the winapi function itself, not sure)
  • Couldnt retrieve the GetLastError at all, in any case it resulted in 0 (probably i havent understood the documentation?)
  • Advantages as effective means of detecting internet
  • doesnt use ping, which might be restricted in some environments & sometimes server response timeout could be misinterpreted
  • InternetGetConnectedState tells u if the PC is configured for internet connection or is it working online but it doesnt always tell u if u can browser internet (typical example, try on a lan connection and use an invalid gateway or ipaddress so that u cant open a webpage, then try the winapi call.) literally the basic difference between what internet explorer calls as 'working offline' and 'web page cannot be displayed'
Edited by rajeshontheweb
Posted (edited)

one thing , this may not be efficient way to check if a domain is online because ping would fail if server is unavailable, but if u use checkconnection, then u still may get true value because there might be a 404 error page or a search directive configured, which literally means internet explorer still would be able to send data to server and retrieve info (which is the sole purpose of the function , as i understand it )

viz., try ww.google.com u would get a positive response !

Edited by rajeshontheweb
Posted

The documentation says, it will work with a NULL-value, not with an empty string. This is a difference. a NULL-string equals a pointer to nothing.

; Author ........: Rajesh V R (thanks to SmOke_N for making me understand the dllcall better)
Func _WinINet_InternetCheckConnection($lpsz_URL, $dw_Flags = $DW_FLAG_ICC_FORCE_CONNECTION, $w_Reserved = 0)
    Local $TypeURL = "ptr"
    If IsString($lpsz_URL) And $lpsz_URL Then $TypeURL = "wstr"
    ; Make DLL call
    Local $varResult = DllCall("wininet.dll", "int", "InternetCheckConnectionW", $TypeURL, $lpsz_URL, "uint", $dw_Flags, "uint", $w_Reserved)
    ; Return response
    If @error Then Return SetError(-1, 0, "")
    Return SetError($varResult[0]=0, 0, $varResult[0])

EndFunc   ;==>_WinINet_InternetCheckConnection

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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