0

I'm checking the Internet connection state and want to send a MessageBox if no internet connection is present what I have is this:

;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
Sleep PROTO STDCALL :DWORD

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

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 Sleep,5000 ;Sleep so it doesn't use 99% CPU
        INVOKE  IsOnline,offset szURL    
        or      eax,eax
        jz      loop00

        INVOKE  Beep,750,1000
        exit

_main   ENDP

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

        END     _main

Now I thought to add something like this

 .data
    MyTitle db "No internet",0
    MyText db "No active internet connection found, retrying in 5 seconds.",0

            push 0
            push offset MyTitle
            push offset MyText
            push 0
            call MessageBoxA

But I can't really figure out where I would have to put that

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
Marius Prollak
  • 368
  • 1
  • 7
  • 22
  • Why are you doing this in MASM?!? – Nik Bougalis Feb 25 '13 at 23:10
  • If you like it, more power to you. I just don't see the point in using assembly for this type of thing. – Nik Bougalis Feb 25 '13 at 23:19
  • 1
    @MariusProllak: what Nik is wondering is why you are choosing such a low-level approach to writing Windows programs, when the gain of writing assembly is negligible. As I mentioned elsewhere, it probably will save you a lot of grief to code this in C, C++, or some other medium- to high-level language. However, if you are busy learning x86 assembler, then that's a different story, and as Nik says, more power to you. I just don't envy you having to write a Windows application in assembler.... – John Källén Feb 26 '13 at 00:01
  • "Why wouldn't I?" _That's mah boy!_ – alecov Feb 26 '13 at 16:32

1 Answers1

1

How about this:

_main   PROC

loop00: 
        INVOKE  IsOnline,offset szURL    
        or      eax,eax
        jnz      done

        push 0
        push offset MyTitle
        push offset MyText
        push 0
        call MessageBoxA

        Invoke Sleep,5000 ;Sleep so it doesn't use 99% CPU

        jmp loop00

done:
        INVOKE  Beep,750,1000
        exit

_main   ENDP

.data
    MyTitle db "No internet",0
    MyText db "No active internet connection found, retrying in 5 seconds.",0

That will first wait test for IsOnline, and if it fails, will wait 5 seconds and try again. Otherwise it goes to the beep before exiting.

John Källén
  • 7,551
  • 31
  • 64
  • I don't think you are supposed to put code into the data section. – Alexey Frunze Feb 25 '13 at 23:12
  • Slipped on the keyboard and posted too soon; should work better now. – John Källén Feb 25 '13 at 23:17
  • So, now the question is... Is it going to show more than one message boxes or is it going to wait indefinitely for a button pressed? I think, both behaviors are undesirable. – Alexey Frunze Feb 25 '13 at 23:20
  • Agreed. It probably would be a better idea to write the "retrying in 5 seconds" to some diagnostic stream rather than bothering the user with dialog. Oh, and a cancel button for MessageBoxA works a treat, so the user can quit the otherwise infinite loop.An alternative is to just show the dialog box once – John Källén Feb 25 '13 at 23:22
  • @JohnKällén Well the thing with the Dialog is, I have this whole IsOnline function on a form that is already drawn, if there's no Internet connection present and it's in the loop, after clicking somewhere on the form the windows loading circle appears, and it says the application is not responding, do you want to close it bla bla. If you could point me in another direction how I can fix this problem, that would be greatly appreciated. – Marius Prollak Feb 25 '13 at 23:44
  • I'd go for a label window on your form which would normally be hidden (ShowWindow(SW_HIDE)) but which would be shown when the net is down. I'd also have a timer on the form that would periodically check for the presence of internet and show/hide the label depending on the result of that check. That way, the status message doesn't become so obtrusive and maybe the user can proceed working in "offline mode". – John Källén Feb 25 '13 at 23:50
  • I'd probably also switch to using C, C++, or some other higher-level language rather than assembler -- I would hate to write anything but the smallest, most performance-dependent fragments of code in assembler. – John Källén Feb 25 '13 at 23:51
  • The actual program will not get loaded unless there's an active internet connection (that's why I check it) would putting a timer on the IsOnline function, rather than putting it in a loop (kill the timer after connection is established) solve the Application not responding issue ? – Marius Prollak Feb 25 '13 at 23:53
  • Another possibility is to check for IsOnline. If that fails, show a dialog "Internet doesn't appear to be accessible. Do you wish to continue trying? [Yes/No]". On No, break out of the loop and terminate. Before displaying the dialog, start a timer that times out every N seconds. In Your main windows loop, look for WM_TIMER messages from that timer, and perform your IsOnline there. If the internet comes up, you can then send a WM_CLOSE to the dialog box and open your form. – John Källén Feb 25 '13 at 23:59