检测网卡是否活动的vc代码

vc中可以使用SENSAPI.DLL中的IsNetworkAlive函数来检测网卡是否活动,msdn中的说明如下:

IsNetworkAlive Function

The IsNetworkAlive function determines whether or not a local system is connected to a network, and identifies the type of network connection, for example, a LAN, WAN, or both.

Syntax
C++
Bool IsNetworkAlive(
  __out  LPDWORD lpdwFlags
);

Parameters
lpdwFlags [out]
The type of network connection that is available. This parameter can be one of the following values:

NETWORK_ALIVE_LAN
The computer has one or more LAN cards that are active.

NETWORK_ALIVE_WAN
The computer has one or more active RAS connections.

Return Value
TRUE If the last error is 0 and the function returns TRUE, SENS has determined that a local system is connected to a network.

FALSE If the last error is 0 and the function returns FALSE, SENS has determined there is no connection.

检测网卡是否活动的vc代码如下:
头文件

#define   NETWORK_ALIVE_LAN   0x00000001  
#define   NETWORK_ALIVE_WAN   0x00000002  
#define   NETWORK_ALIVE_AOL   0x00000004  

typedef   BOOL   (CALLBACK   *ISNETWORKALIVE)(LPDWORD   lpdwFlags);

class CCheckNetAlive : public CObject  
{
public:
    CCheckNetAlive();
    virtual ~CCheckNetAlive();

public:
    HINSTANCE   hinstLib;  
    ISNETWORKALIVE IsNetworkAlive;
public:
    BOOL IsNetAlive();
};


cpp文件

CCheckNetAlive::CCheckNetAlive()
{
    hinstLib  =  LoadLibrary("SENSAPI.DLL");
    if(hinstLib)
    {
        IsNetworkAlive=(ISNETWORKALIVE)GetProcAddress(hinstLib, "IsNetworkAlive");

        if(IsNetworkAlive==NULL)
        {
            printf("载入函数IsNetworkAlive失败!");
        }
    }
    else
    {
        printf("载入库SENSAPI.DLL失败!");
    }
}

CCheckNetAlive::~CCheckNetAlive()
{
    if(hinstLib)  
        FreeLibrary(hinstLib);  
}

BOOL CCheckNetAlive::IsNetAlive()
{
    if(IsNetworkAlive!=NULL)
    {
        DWORD   dwFlag   =   0;  
        
        BOOL bRes = IsNetworkAlive(&dwFlag);
        if(bRes)
        {
            if((dwFlag&NETWORK_ALIVE_WAN) || (dwFlag&NETWORK_ALIVE_LAN))  
                return TRUE;      
            else  
                return FALSE;  
        }
        else
        {
            return FALSE;  
        }
    }

    return FALSE;
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 代码 网卡
相关日志:
评论: 1 | 引用: 0 | 查看次数: -
回复回复小辉网[2009-09-11 04:07 PM | del]
我对vc很有兴趣,无奈不能入门,看你的源码希望有收获
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.