News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Check for internet connection

Started by Magnum, October 10, 2011, 04:38:56 PM

Previous topic - Next topic

Gunner

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
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

Magnum

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

Have a great day,
                         Andy

Magnum

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 ?
Have a great day,
                         Andy

Magnum

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
Have a great day,
                         Andy

Magnum

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
Have a great day,
                         Andy

dedndave

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

dedndave

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

Gunner

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
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

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

Magnum

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.




Have a great day,
                         Andy

Gunner

Hmm, modems have/get ips right?  So it should work.  Dunno, haven't had a modem in years.
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

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

Magnum

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.

:-)
Have a great day,
                         Andy

Gunner

Nah, with this USB 3 on the new box, the WiFi dongle gets 850KBs transfer  :toothy and the transmitter is 2 floors below...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

Tedd

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.
No snowflake in an avalanche feels responsible.