CE获取Wifi的设备名、连接状态和信号强度

 

 

CE 获取 Wifi 的设备名、连接状态和信号强度
 

CE获取Wifi的设备名、连接状态和信号强度

Note:需CE4.0或更高版本(兼容NDIS5.1)支持
一、加入头文件
#include <winioctl.h>
#include <ntddndis.h>
#include <nuiouser.h>
二、Attach to NDISUIO
HANDLE hNdis = ::CreateFile( NDISUIO_DEVICE_NAME, GENERIC_ALL, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
(HANDLE)INVALID_HANDLE_VALUE );

三、获取设备名称

UCHAR                  cbQueryBuffer[ 1024 ];
PNDISUIO_QUERY_BINDING pQueryBinding;
DWORD                  dwBytesReturned = 0;
pQueryBinding = (PNDISUIO_QUERY_BINDING)&cbQueryBuffer[ 0 ];
pQueryBinding->BindingIndex = 0;
if ( ::DeviceIoControl( hNdis,
IOCTL_NDISUIO_QUERY_BINDING,
(LPVOID)&cbQueryBuffer[ 0 ],
sizeof(NDISUIO_QUERY_BINDING),
(LPVOID)&cbQueryBuffer[ 0 ],
sizeof(cbQueryBuffer),
&dwBytesReturned,
NULL ) == TRUE )
{
TCHAR* pDeviceName = (TCHAR*)( cbQueryBuffer + pQueryBinding->DeviceNameOffset );
}

 

四、检测连接状态

NIC_STATISTICS nicStatistics = { 0 };
DWORD          dwBytesReturned = 0;
BOOL bConnected = FALSE;
nicStatistics.ptcDeviceName = pDeviceName;
if ( ::DeviceIoControl( hNdis,
IOCTL_NDISUIO_NIC_STATISTICS,
NULL,
0,
&nicStatistics,
sizeof(NIC_STATISTICS),
&dwBytesReturned,
NULL ) == TRUE )
{
bConnected = ( nicStatistics.MediaState == MEDIA_STATE_CONNECTED );
}

五、获取信号强度

// example.
// < -90 : No Signal
// < -81 : Very Low
// < -71 : Low
// < -67 : Good
// < -57 : Very Good
// ...   : Excellent
NDISUIO_QUERY_OID ndisQueryOid = { 0 };
DWORD             dwBytesReturned = 0;
int nDb = 0;
ndisQueryOid.Oid = OID_802_11_RSSI;
ndisQueryOid.ptcDeviceName = pDeviceName;
if ( ::DeviceIoControl( hNdis,
IOCTL_NDISUIO_QUERY_OID_VALUE,
(LPVOID)&ndisQueryOid,
sizeof(ndisQueryOid),
(LPVOID)&ndisQueryOid,
sizeof(ndisQueryOid),
&dwBytesReturned,
NULL ) == TRUE )
{
::CopyMemory( &nDb, &ndisQueryOid.Data[ 0 ], sizeof(ULONG) );
}
 
又一篇文章获取信号强度:

void OnBnClickedButtonWifi()
{
    HANDLE hNdis = ::CreateFile( NDISUIO_DEVICE_NAME, GENERIC_ALL, 0, NULL, OPEN_EXISTING,
                             FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
                             (HANDLE)INVALID_HANDLE_VALUE );


    //DEVICE NAME
    UCHAR                  cbQueryBuffer[ 1024 ];
    PNDISUIO_QUERY_BINDING pQueryBinding;
    DWORD                  dwBytesReturned = 0;
    TCHAR* pDeviceName;

    pQueryBinding = (PNDISUIO_QUERY_BINDING)&cbQueryBuffer[ 0 ];
    pQueryBinding->BindingIndex = 0;

    if ( ::DeviceIoControl( hNdis,
                            IOCTL_NDISUIO_QUERY_BINDING,
                            (LPVOID)&cbQueryBuffer[ 0 ],
                            sizeof(NDISUIO_QUERY_BINDING),
                            (LPVOID)&cbQueryBuffer[ 0 ],
                            sizeof(cbQueryBuffer),
                            &dwBytesReturned,
                            NULL ) == TRUE )
    {
        pDeviceName = (TCHAR*)( cbQueryBuffer + pQueryBinding->DeviceNameOffset );
    }
#if 0
    //CONNECT STATUS
    NIC_STATISTICS nicStatistics = { 0 };    
    BOOL bConnected = FALSE;

    nicStatistics.ptcDeviceName = pDeviceName;

    if ( ::DeviceIoControl( hNdis,
                            IOCTL_NDISUIO_NIC_STATISTICS,
                            NULL,
                            0,
                            &nicStatistics,
                            sizeof(NIC_STATISTICS),
                            &dwBytesReturned,
                            NULL ) == TRUE )
    {
        if( nicStatistics.MediaState == MEDIA_STATE_CONNECTED )
            m_edit1.SetWindowTextW(L"Connected");    
        else
            m_edit1.SetWindowTextW(L"not connect");
    }
#endif

    NDISUIO_QUERY_OID ndisQueryOid = { 0 };
    int nDb = 0;


    ndisQueryOid.Oid = OID_802_11_RSSI;
    ndisQueryOid.ptcDeviceName = pDeviceName;

    if ( ::DeviceIoControl( hNdis,
                            IOCTL_NDISUIO_QUERY_OID_VALUE,
                            (LPVOID)&ndisQueryOid,
                            sizeof(ndisQueryOid),
                            (LPVOID)&ndisQueryOid,
                            sizeof(ndisQueryOid),
                            &dwBytesReturned,
                            NULL ) == TRUE )
    {
        ::CopyMemory( &nDb, &ndisQueryOid.Data[ 0 ], sizeof(ULONG) );
    }

    CString str;
    str.Format(L"%d",ndisQueryOid.Data[ 0 ]);
    
    m_edit1.SetWindowTextW(str);
    CloseHandle(hNdis);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值