CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2001
    Location
    Canada
    Posts
    22

    Check network connection availability

    Hi, All

    I was trying to check the network connection availability using API function IsNetworkAlive, but no matter my computer is or not connected to the network, it always returns true to me, which means the local system is connected to the network. Could anybody ever used this function before help me out?

    Thanks

  2. #2
    Join Date
    May 2002
    Location
    Somewhere over the rainbow
    Posts
    423
    u can try few tricks, using winsock, try connect to the net:

    GetHostByName("http://www.codeguru.com");
    return value Zero if cannot connect to host.

    note: use winsock reference for APIs.
    Bengi

  3. #3
    Join Date
    Oct 2001
    Location
    Canada
    Posts
    22
    Thanks, Bengi. I know there are several different ways to check network connection availability.
    It is just I saw this IsNetworkAlive function on MSDN and want to tyr it out. But strangely it doesn't work the way it is supposed to be. So I was wondering if somebody has used this before. If they got the same problem, maybe it is a bug of Microsoft platform SDK.

  4. #4
    Join Date
    Nov 2001
    Posts
    69
    For the sake of curiosity, I tried the method and worked properly as expected for me without any problems. May be I could help you if you describe what exactly is happening in your case.

  5. #5
    Join Date
    Oct 2001
    Location
    Canada
    Posts
    22
    It is just simply several lines of code I wrote to test this function.

    char szBuf[1024];
    DWORD dwFlag = 0;

    if( IsNetworkAlive( &dwFlag ) )
    {
    switch( dwFlag )
    {
    case NETWORK_ALIVE_LAN:
    MessageBox( NULL, "Computer is connected to LAN", "Connected", MB_OK );
    break;

    case NETWORK_ALIVE_WAN:
    MessageBox( NULL, "Computer is connected to WAN", "Connected", MB_OK );
    break;

    case NETWORK_ALIVE_AOL:
    MessageBox( NULL, "Computer is connected to AOL network", "Connected", MB_OK );
    break;
    }
    }
    else
    {
    sprintf( szBuf, "Can not get network status, error code is %d", GetLastError( ) );
    MessageBox( NULL, szBuf, "Not connected", MB_OK );
    }

    My computer is connected to company LAN, first I ran this code, the function IsNetworkAlive returns true and dwFlag returned is NETWORK_ALIVE_LAN.

    then I unplug the LAN cable, ran it again, I am expecting this function will return false to me, but strangely it still returns true and the dwFlag is NETWORK_ALIVE_LAN.

    Well, maybe this function is only used to determine what kinds of network connectivity is configured for this computer, isn't used for detecting if network connectivity is available or not.

  6. #6
    Join Date
    Nov 2001
    Posts
    69
    Hi Robin, The API IsNetworkAlive takes the address of the DWORD as a parameter, but the point here is the parameter is [in] rather than [out] as most of the pointers in the parameter list represent generally. Initialize the dwFlag to 1 (which is the value of NETWORK_ALIVE_LAN) and try agiain.

    Here we check whether the computer is connected to LAN or WAN or to the AOL n/w and not find out to which type of connection the machine is connected to. As the machine can be connected to more than one type of connection at the same time, it may not be appropriate to get the type of connection from the API.

    Done?

    -TeeJBee

  7. #7
    Join Date
    Oct 2001
    Location
    Canada
    Posts
    22
    Hi, teejbee. Thanks for your reply

    I wouldn't think the LPDWORD parameter here is an [in] parameter rather than a [out] paramter. Here is the explanation for this paramter:

    LPDWORD lpdwFlags
    Provides information on the type of network connection available when the return value is TRUE. The flags can be:
    ...


    As a matter of fact, I did try to initialize the parameter to NETWORK_ALIVE_LAN, but still the same thing happened.

  8. #8
    Join Date
    Nov 2001
    Posts
    69

    Exclamation

    Hi, The following lines are taken from MSDN library.
    Note it may not always be practical for applications to call IsNetworkAlive to determine whether the local system is currently disconnected from a LAN. In this case, IsNetworkAlive can be slow, and it may take too much time for the function to detect that the local system has become disconnected...
    This function is only available for TCP/IP connections.
    MSDN says the parameter as [in]. May n/w is slow or something strange happening!
    Anyway the msdn link is here.
    http://msdn.microsoft.com/library/de...tworkalive.asp
    -TeeJBee
    All The Best

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured