How do I know if I have an Internet Connection?

Started by
2 comments, last by hplus0603 17 years, 4 months ago
I'm using a little MFC and recently made the switch from FTP to HTTP (much faster btw). It turns out, however, that CInternetSession::OpenURL() will crash if the prog does not have an Internet connection. OpenURL will crash before returning, it is supposed to throw a CInternetException, but I am unable to catch it because it crashes too early. This may only happen in my debug build, I haven't tested with release yet. So how can I tell if I'm online. I tried using QueryOption:
PatchInetSession->QueryOption(INTERNET_OPTION_CONNECTED_STATE, dwconnection);
if ((dwconnection & INTERNET_STATE_CONNECTED) == 0)
	return -1;
But (dwconnection) becomes INTERNET_STATE_CONNECTED regardless of the connection status. Any ideas?
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
Advertisement
I suppose that you only check whether you're connected to a network - i.e. if a cable is plugged into your network adapter and there's some kind of network equipment on the other end.
Basically it's hard for a computer to know how large a network it's connected to. I think the only solution is to try connecting to the remote host you're interested in, and see whether it fails or not. Maybe doing it in another thread if you don't want it to block your main one? (I don't know how MFC works btw)
-- Rasmus Neckelmann
InternetCheckConnection is a great function.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
If you can't get that to work, the catch-all would be to use structured exception handling.

__try {  crash goes here}__except(EXCEPTION_EXECUTE_HANDLER) {  oops, I crashed!  clean up and continue}


There are some caveats with SEH vs regular exceptions vs objects with destructors, and you have no guarantee that your heap will be in a useable state after the crash is caught, but you can at least bring up a dialog box and exit the program cleanly in the exception handler.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement