wince6.0 WiFi ON/OFF控制

wince6.0 WiFi ON/OFF控制

分类: wince系统开发 wince下wifi开发 wince驱动开发 8人阅读 评论(0) 收藏 编辑 删除

欢迎加入Wince技术讨论群QQ#326444254

#include "Iphlpapi.h"
#include "eaputil.h"
#include "wzcsapi.h"
#include "ndispwr.h"


WCHAR *szAdapter1=_T("SDIO86861");

// use statically allocated memory block
// to reduce risk of mem leaking from this stress code.
#define MEM_BLOCK_8K_SIZE (1024*8)
UCHAR g_ucBulkMem[MEM_BLOCK_8K_SIZE];


PIP_ADAPTER_INFO GetAdapterInfo(IN WCHAR *szAdapter1) 
{
 RETAILMSG(MSG_ENTER, (_T("GetAdapterInfo(),WiFi adapter = %s/n"), szAdapter1));

    ULONG ulSizeAdapterInfo = MEM_BLOCK_8K_SIZE;
    PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)g_ucBulkMem;
    DWORD dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo);
    if (dwStatus != ERROR_SUCCESS)
        return NULL;
    char szAdapter1A[64];
    wcstombs(szAdapter1A, szAdapter1, 64);
    while(pAdapterInfo != NULL)
    {
        if(_stricmp(szAdapter1A, pAdapterInfo->AdapterName) == 0)
            return pAdapterInfo;
        pAdapterInfo = pAdapterInfo->Next;
    }

 return NULL;

}

BOOL AdapterExist(IN WCHAR *szAdapter1)

{
    return GetAdapterInfo(szAdapter1) != NULL;
}

//
// Execute an NDIS IO control operation.
//
BOOL DoNdisIOControl(IN DWORD dwCommand,IN LPVOID pInBuffer,IN DWORD cbInBuffer,IN LPVOID pOutBuffer,IN DWORD *pcbOutBuffer OPTIONAL)
{
    HANDLE hNdis;
    BOOL bResult = FALSE;
    DWORD cbOutBuffer;

    hNdis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL, OPEN_ALWAYS, 0, NULL);

    if (hNdis != INVALID_HANDLE_VALUE)
    {
        cbOutBuffer = 0;
        if (pcbOutBuffer)
            cbOutBuffer = *pcbOutBuffer;

        bResult = DeviceIoControl(hNdis,
                        dwCommand,
                        pInBuffer,
                        cbInBuffer,
                        pOutBuffer,
                        cbOutBuffer,
                        &cbOutBuffer,
                        NULL);

        if (bResult == FALSE)
            wprintf(L"IoControl result=%d", bResult);

        if (pcbOutBuffer)
            *pcbOutBuffer = cbOutBuffer;

        CloseHandle(hNdis);
    }
    else
    {
        RETAILMSG(MSG_ERROR, (_T("CreateFile of '%s' failed, error=0x%X (%d)/n"), DD_NDIS_DEVICE_NAME, GetLastError(), GetLastError()));
    }

    return bResult;
}


void AdapterPowerOnOff(WCHAR *szAdapter, BOOL bTurnPowerOn)   // TRUE->will cause adapter power on
{
    if(!szAdapter)
        return;

    WCHAR multiSz[256];
    DWORD cbBuffer = sizeof(multiSz);
  
    wcscpy(multiSz, szAdapter);
    multiSz[wcslen(multiSz)+1] = L'/0'; // Multi sz needs an extra null

    // Inform ndispwr.dll on the power state of this adapter..
    HANDLE hNdisPwr = CreateFile(
                        NDISPWR_DEVICE_NAME,
                        0x00,
                        0x00,
                        NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                        (HANDLE)INVALID_HANDLE_VALUE); 

    if (hNdisPwr != INVALID_HANDLE_VALUE)
    {
        NDISPWR_SAVEPOWERSTATE SavePowerState;
        SavePowerState.pwcAdapterName = szAdapter;
        SavePowerState.CePowerState = bTurnPowerOn ? PwrDeviceUnspecified : D4;

        DeviceIoControl(
            hNdisPwr,
            IOCTL_NPW_SAVE_POWER_STATE,
            &SavePowerState,
            sizeof(NDISPWR_SAVEPOWERSTATE),
            NULL,
            0x00,
            NULL,
            NULL);   

        CloseHandle(hNdisPwr);
    }

    // Determine if we support power managment  
    CEDEVICE_POWER_STATE Dx = PwrDeviceUnspecified;
    WCHAR szPwrName[MAX_PATH];
    wsprintf(szPwrName, (_T("%s//%s"), PMCLASS_NDIS_MINIPORT, szAdapter));
    szPwrName[wcslen(szPwrName)+1] = 0;

    GetDevicePower(szPwrName, POWER_NAME, &Dx);
    BOOL bSupportsPowerManagment = (PwrDeviceUnspecified < Dx && PwrDeviceMaximum > Dx);

    
    if(!bSupportsPowerManagment)
        OutputDebugString(L"not fSupportsPowerManagment/n");

    // Issue the PwrDeviceUnspecified or D4 to the adapter if it supports power management.
    if (bSupportsPowerManagment)
    {
        if(ERROR_SUCCESS !=
            SetDevicePower(
                szPwrName, POWER_NAME,
                bTurnPowerOn ? PwrDeviceUnspecified : D4)
        )
            OutputDebugString(L"SetDevicePower() error/n");
    }

    DoNdisIOControl(
        bTurnPowerOn ? IOCTL_NDIS_BIND_ADAPTER : IOCTL_NDIS_UNBIND_ADAPTER,
        multiSz,
        (wcslen(multiSz)+2) * sizeof(WCHAR),
        NULL,
        NULL);
}

sources文件里添加所需文件:
    wzcsapi.lib  
    iphlpapi.lib 


调用:
AdapterPowerOnOff(szAdapter1,1);//打开
AdapterPowerOnOff(szAdapter1,0);//关闭
及其简单,关闭后,在系统中找不到所关闭的ADAPTER,非常干净! 不过前提是WIFI的接口驱动(我的接的HSMMC0)要能够工作正常,我的HSMMC0就因为CH0没有检测脚而导致检测wifi模块的insert和remove走了不少弯路。

查看评论

  暂无评论

发表评论
  • 用 户 名:
  • u011610939
  •   
* 以上用户言论只代表其个人观
  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值