The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Magnum on October 10, 2011, 04:38:56 PM

Title: Check for internet connection
Post by: Magnum on October 10, 2011, 04:38:56 PM
I would like a program to start based on whether there is a current internet connection.

How would I be able to check for that ?

Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 04:54:46 PM
i know just what you mean - lol

when i have a connection, i don't want any damned balloons popping up out of the task bar telling me so
when i do not have a connection, i want an alert telling me when it comes back on - only one alert   :P
Title: Re: Check for internet connection
Post by: baltoro on October 10, 2011, 05:44:47 PM
MAGNUM,   
I once wrote an application to do this in C++ and assembly. You can either check the Registry entries that are made by your network device, or you can use WMI (which is a COM interface) to do this. In my application, I did both, and found that it was reliable. The only annoying aspect of doing this is that you have to poll the data sources,...which means that once you activate the application, you are letting it run in the background.
I have forgotten exactly where the registry entries are located,...at the time, I had found a source on the Internet that described this operation, and I used SysInternals RegMon to verify the correct keys. When you are NOT connected to the Internet, these Registry keys (and the data values) are deleted from the Registry (at least that was how it worked with my machine -- Windows XP -- using the wireless network adapter).
...Let me search,...and, I'll see what I can find,...
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 05:56:38 PM
I came up with this, but it is not working.



.DATA

Connected   db   "Connected.",0
AppName     db   " ",0

.data?

l_RASCONN RASCONN 0FFh dup ({})
l_Buffer_Size dd ?
l_Conn_Count dd ?
hand1  dd ?
hand2  dd ?

.CODE

Start:

invoke RasEnumConnections, addr l_RASCONN, addr l_Buffer_Size, addr l_Conn_Count
mov  hand1,eax


invoke CreateEvent,0,FALSE,FALSE,0
mov  hand2,eax

invoke RasConnectionNotification,hand1,hand2,RASCN_Connection ; check if there is an internet connection

.IF (eax == 0)

invoke  MessageBox, NULL, addr Connected, addr AppName, MB_OK

.ENDIF


; invoke Sleep,3000 ; give the system enuf time to end the connection
                  ; don't want to leave the port in an inconsistent state.

invoke ExitProcess, 0

END Start
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 06:02:57 PM
Quote from: baltoro on October 10, 2011, 05:44:47 PM
MAGNUM,   
I once wrote an application to do this in C++ and assembly. You can either check the Registry entries that are made by your network device,
...Let me search,...and, I'll see what I can find,...

I looked thru the registry to find any entries that are made when I am connected.

Could not find anything.

Title: Re: Check for internet connection
Post by: baltoro on October 10, 2011, 06:03:05 PM
MAGNUM,
I didn't use that RasEnumConnections API in my code.
What I did was Google for the manufacturer name of my Network device, and then determine the correct Internet protocol that it uses.
And, yeah, the Registry entries are really obscure,...as I recall, I had no clue where they might be located until I found the info on the Web,...but, what I finally found was: Dynamic Host Configuration Protocol. I think if you do a Registry search for: DHCP when you are connected, you will it,...
I'm still searching, by the way,...
Sorry, I'm at a public terminal and it's logging me off
Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 06:12:18 PM
there should be a simple way to ping something   :P
notice, that a viable connection to a router does not mean the internet is available
maybe you could ping http://www.google.com
they are pretty fast and reliable
Title: Re: Check for internet connection
Post by: ragdog on October 10, 2011, 06:24:12 PM
InternetCheckConnection
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346%28v=vs.85%29.aspx

I hope it help
Title: Re: Check for internet connection
Post by: NoCforMe on October 10, 2011, 06:24:58 PM
I would think that the registry would be the last place one would want to look for internet connection information.

Keep in mind that not all of us have networks. I have a dial-up connection; how would you go about determining if I'm connected? (Remember, you want a general solution that works for everyone, not just your computer and your internet connection.)
Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 06:50:47 PM
be sure to look at the end of the page that ragdog linked for you
there is a little example that may be helpful
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 08:22:16 PM
Thanks to everyone.

I don't understand this. Can I use Null, or do I have to ping an actual internet address ?

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.
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 08:35:26 PM
invoke InternetCheckConnection,NULL,FLAG_ICC_FORCE_CONNECTION,0

gives me :

C:\masm32\SOURCE\Internet_Chk_Conn.asm(38) : error A2006:undefined symbol : FLAG_ICC_FORCE_CONNECTION
C:\masm32\SOURCE\Internet_Chk_Conn.asm(38) : error A2114:INVOKE argument type mismatch : argument : 2
Title: Re: Check for internet connection
Post by: jj2007 on October 10, 2011, 08:38:08 PM
My favourite is InternetGetConnectedState (http://support.microsoft.com/kb/242558).
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 08:47:54 PM
I don't understand what this is.

I know that I want INTERNET_CONNECTION_MODEM.

lpdwFlags
[out] Pointer to a variable that receives the connection description. This parameter can be one or more of the following values.
Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 08:59:02 PM
i would define FLAG_ICC_FORCE_CONNECTION like this
IFNDEF  FLAG_ICC_FORCE_CONNECTION
FLAG_ICC_FORCE_CONNECTION EQU 1
ENDIF

that way, if a future version of windows.inc or wininet.inc has the flag, it will still assemble correctly
you might also make a mention in the masm32 windows.inc subforum - there are probably several similar flags missing, too
or you could just use "1"   :bg

QuotelpdwFlags
[out] Pointer to a variable that receives the connection description. This parameter can be one or more of the following values.

create a dword variable and pass the address of it to the function
after a successful call, the dword will hold one of those values
Title: Re: Check for internet connection
Post by: Gunner on October 10, 2011, 08:59:06 PM
Don't remember where I got this, but works well.  Returns 0 if not connected.


CurrentlyOnline PROC

LOCAL szHostName[256] :BYTE

invoke gethostname, ADDR szHostName, 256
.IF eax == SOCKET_ERROR
xor eax, eax
ret
.ENDIF
invoke gethostbyname, addr szHostName
.IF ! eax
xor eax, eax
ret
.ENDIF
mov eax, dword ptr [eax + 12]
mov eax, dword ptr [eax]
mov eax, dword ptr [eax]
sub eax, 1*256*256*256 + 127
ret
CurrentlyOnline ENDP
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 10:08:35 PM
This is not my day for getting stuff to work. :-)


start:

call CurrentlyOnline

invoke ExitProcess,0

CurrentlyOnline PROC

LOCAL szHostName[256] :BYTE

invoke gethostname, ADDR szHostName, 256
.IF eax == SOCKET_ERROR
xor eax, eax
ret
.ENDIF
invoke gethostbyname, addr szHostName
.IF ! eax
xor eax, eax
ret
.ENDIF
mov eax, dword ptr [eax + 12]
mov eax, dword ptr [eax]
mov eax, dword ptr [eax]
sub eax, 1*256*256*256 + 127
ret
CurrentlyOnline ENDP


end start

Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 10:34:17 PM
Quote
lpdwFlags
[out] Pointer to a variable that receives the connection description. This parameter can be one or more of the following values.

create a dword variable and pass the address of it to the function
after a successful call, the dword will hold one of those values

Been a long day and my head is sore from banging it on the keyboard.

Could you help me some more. I know how to make a dword variable.

Something dw ?
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 10:40:26 PM
I think I have got this figured out.

Now for some bandages.


IFNDEF  INTERNET_CONNECTION_MODEM
INTERNET_CONNECTION_MODEM EQU 1
ENDIF

.DATA

Connected   db   "Connected.",0
AppName     db   " ",0


.data?

Storage  dw  ?

.CODE

Start:

invoke  InternetGetConnectedState,addr Storage ,0

.IF (eax == 1)

invoke  MessageBox, NULL, addr Connected, addr AppName, MB_OK

.ENDIF

invoke ExitProcess, 0

END Start
Title: Re: Check for internet connection
Post by: Magnum on October 10, 2011, 11:23:23 PM
Is this the flag to use for a connections using a DSL modem?

MSDN explanations are terse and cryptic at times.

INTERNET_CONNECTION_LAN
0x02
Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 11:27:56 PM
QuoteCould you help me some more. I know how to make a dword variable.

Something dw ?

Somthing dd ?

DD stands for "define dword"
DW stands for "define word"


not sure what the value would be for DSL - it's a modem, so guessing .......MODEM
but, it could be LAN if windows sets it up that way - never had DSL   :P
Title: Re: Check for internet connection
Post by: dedndave on October 10, 2011, 11:50:28 PM
for Rob's code, you need
        INCLUDE    \masm32\include\ws2_32.inc
        INCLUDELIB \masm32\lib\ws2_32.lib


but i get nothing out of that one - should work, too
i must be doing something wrong, so don't listen to me   :bg
Title: Re: Check for internet connection
Post by: Gunner on October 11, 2011, 12:00:21 AM
Um, how a bout wsock32.inc?

If you are not online, the return value is 0, if you are online, then it returns non zero.  I came to use that because users told me the other ways don't or don't always work.  This code works down to 95 I believe
Title: Re: Check for internet connection
Post by: dedndave on October 11, 2011, 12:01:38 AM
tried that, too, Rob - i don't get past gethostname

i even tried putting "127.0.0.1",0 in a string and calling gethostbyname   :P
Title: Re: Check for internet connection
Post by: Magnum on October 11, 2011, 12:03:27 AM
I could not get Rob's code to work.

Maybe it doesn't work for a modem connection.

Storage  dw  ? worked, but I will change it to dd.




Title: Re: Check for internet connection
Post by: Gunner on October 11, 2011, 12:05:20 AM
Hmm, modems have/get ips right?  So it should work.  Dunno, haven't had a modem in years.
Title: Re: Check for internet connection
Post by: dedndave on October 11, 2011, 12:11:41 AM
yah - it should work

this one seems to fly...
        .XCREF
        .NOLIST
        INCLUDE    \masm32\include\masm32rt.inc
        INCLUDE    \masm32\include\wininet.inc
        INCLUDELIB \masm32\lib\wininet.lib
        .LIST

INTERNET_CONNECTION_MODEM      EQU 1
INTERNET_CONNECTION_LAN        EQU 2
INTERNET_CONNECTION_PROXY      EQU 4
INTERNET_CONNECTION_MODEM_BUSY EQU 8
INTERNET_RAS_INSTALLED         EQU 10h
INTERNET_CONNECTION_OFFLINE    EQU 20h
INTERNET_CONNECTION_CONFIGURED EQU 40h

;-------------------------------------------------------------------------

        .CODE

_main   PROC

        xor     eax,eax
        push    eax                             ;make space on stack for dwFlags
        mov     edx,esp                         ;EDX = lpdwFlags
        INVOKE  InternetGetConnectedState,edx,eax
        print   uhex$(eax),32
        pop     eax                             ;dwFlags from stack
        print   uhex$(eax),13,10
        inkey
        exit

_main   ENDP

;-------------------------------------------------------------------------

        END     _main


results if not connected
00000000 00000010
dwFlags = INTERNET_RAS_INSTALLED

results if connected
00000001 00000012
dwFlags = INTERNET_RAS_INSTALLED or INTERNET_CONNECTION_LAN

however, i did not verify the results if the LAN is up, but the internet is down
seems to me that it would still report LAN present
Title: Re: Check for internet connection
Post by: Magnum on October 11, 2011, 02:00:21 AM
Quote from: Gunner on October 11, 2011, 12:05:20 AM
Hmm, modems have/get ips right?  So it should work.  Dunno, haven't had a modem in years.

Would like to buy one ?

I have plenty to sell.

:-)
Title: Re: Check for internet connection
Post by: Gunner on October 11, 2011, 02:08:05 AM
Nah, with this USB 3 on the new box, the WiFi dongle gets 850KBs transfer  :toothy and the transmitter is 2 floors below...
Title: Re: Check for internet connection
Post by: Tedd on October 11, 2011, 01:02:20 PM
There are various settings you could check, some of which will work for certain set-ups, and others for other set-ups..
But the only method I can think of that's guaranteed to work in all cases is to ping 3 reliable sites - if any of them replies then you're online, or timeout and decide you're not.
Title: Re: Check for internet connection
Post by: FORTRANS on October 11, 2011, 01:17:45 PM
Quote from: dedndave on October 11, 2011, 12:11:41 AM
yah - it should work

this one seems to fly...

Hi,

   Here are my results.

HTH,

Steve


Fresh reboot, cable not plugged in.

G:\WORK>test34
00000000 00000010
Press any key to continue ...

Plugged cable into live DSL modem with no internet.

G:\WORK>test34
00000001 00000012
Press any key to continue ...

Waited for internet connection to come up.

G:\WORK>test34
00000001 00000012
Press any key to continue ...
Title: Re: Check for internet connection
Post by: dedndave on October 11, 2011, 01:32:04 PM
just as i suspected

however, if you look at the link that Jochen posted, it's only the first step...

http://www.masm32.com/board/index.php?topic=17520.msg147244#msg147244

that part is merely used to avoid the connection dialog
the next step is InternetOpenURL

if (InternetGetConnectedState(...) == FALSE)
{
    // Don't attempt connection or it will bring up the dialog
    ...
}
else
{
    //Attempt connection
    if (InternetOpenURL(...) == NULL)
    {
        // Call failed
        err = GetLastError();
        if ((err == ERROR_INTERNET_NAME_NOT_RESOLVED) ||
            (err == ERROR_INTERNET_CANNOT_CONNECT) ||
            (err == ERROR_INTERNET_TIMEOUT))
        {
            // probably not connected...handle appropriately
            ...
        }
    }
    // We're connected!!!
    ....
}


i think some code is missing, like InternetOpen, InternetCloseHandle   :P
Title: Re: Check for internet connection
Post by: dedndave on October 11, 2011, 01:54:20 PM
rather than doing all that...

InternetGetConnectedState
InternetCheckConnection

look ma, no handle   :bg
Title: Re: Check for internet connection
Post by: dedndave on October 11, 2011, 02:30:57 PM
this works for me
if i have a connection to the router, but the router is not connected to the internet, it returns FALSE

        .XCREF
        .NOLIST
        INCLUDE    \masm32\include\masm32rt.inc
        INCLUDE    \masm32\include\wininet.inc
        INCLUDELIB \masm32\lib\wininet.lib
        .LIST

IFNDEF FLAG_ICC_FORCE_CONNECTION
FLAG_ICC_FORCE_CONNECTION EQU 1
ENDIF

IsOnline PROTO  :LPSTR

        .DATA

szURL   db 'http://www.google.com',0

;-------------------------------------------------------------------------

        .CODE

_main   PROC

        INVOKE  IsOnline,offset szURL
        print   uhex$(eax),13,10
        inkey
        exit

_main   ENDP

;-------------------------------------------------------------------------

IsOnline PROC   lpszURL:LPSTR

;Test Internet Connection - DednDave, 10-2011
;
;lpszURL points to a zero-terminated test URL string (must start with "http://")
;
;Returns EAX = FALSE if not connected
;        EAX = TRUE if connected

        push    eax
        mov     edx,esp
        INVOKE  InternetGetConnectedState,edx,0
        or      eax,eax
        jz      IsOnl0

        INVOKE  InternetCheckConnection,lpszURL,FLAG_ICC_FORCE_CONNECTION,0

IsOnl0: pop     edx
        ret

IsOnline ENDP

;-------------------------------------------------------------------------

        END     _main
Title: Re: Check for internet connection
Post by: baltoro on October 11, 2011, 07:31:07 PM
You can also use WMI (Windows Management Instrumentation) to determine various hardware configuration properties of your local machine. Check out: CIM Studio (http://technet.microsoft.com/en-us/library/cc181062.aspx). CIM Studio is a free MSDN utility that is the easiest tool for browsing WMI. CIM Studio is available as part of the WMI tools.
The well-known assembly genius,...EDGAR,...has posted an example for accessing the WMI interface in assembly language: WMI (http://www.masm32.com/board/index.php?topic=4171.0). WARNING: WMI is a COM interface, and, always runs as an out-of-proc COM server...it requires a special COM security setting, which EDGAR demonstrates in his code example. Here is an explanation of COM activation: How Does COM Activation Work Anyway? (http://blogs.msdn.com/b/larryosterman/archive/2004/10/12/241420.aspx)
Title: Re: Check for internet connection
Post by: dedndave on November 13, 2011, 02:03:03 PM
that worked great
had cable service drop out for about half an hour
ran this little program to tell me when it came back on   :bg

;build as a WINDOWS app

        .XCREF
        .NOLIST
        INCLUDE    \masm32\include\masm32rt.inc
        INCLUDE    \masm32\include\wininet.inc
        INCLUDELIB \masm32\lib\wininet.lib
        .LIST

;-------------------------------------------------------------------------

IsOnline PROTO   :LPSTR

;-------------------------------------------------------------------------

IFNDEF FLAG_ICC_FORCE_CONNECTION
FLAG_ICC_FORCE_CONNECTION EQU 1
ENDIF

;-------------------------------------------------------------------------

        .CODE

IsOnline PROC    lpszURL:LPSTR

;Test Internet Connection
;
;lpszURL points to a zero-terminated test URL string (must start with "http://")
;
;Returns EAX = FALSE if not connected
;            = TRUE if connected
;        EDX = connection description (see InternetGetConnectedState documentation)

        push    eax
        mov     edx,esp
        INVOKE  InternetGetConnectedState,edx,0
        or      eax,eax
        jz      IsOnl0

        INVOKE  InternetCheckConnection,lpszURL,FLAG_ICC_FORCE_CONNECTION,0

IsOnl0: pop     edx
        ret

IsOnline ENDP

;-------------------------------------------------------------------------

szURL   db 'http://www.google.com',0

;-------------------------------------------------------------------------

_main   PROC

loop00: INVOKE  IsOnline,offset szURL
        or      eax,eax
        jz      loop00

        INVOKE  Beep,750,1000
        exit

_main   ENDP

;-------------------------------------------------------------------------

        END     _main
Title: Re: Check for internet connection
Post by: ragdog on November 13, 2011, 03:27:33 PM
Hi magnum

Here is a older code for check connection
http://www.masm32.com/board/index.php?topic=6200.msg46365#msg46365