C++ 使用wlanapi控制无线网卡开关

 使用wlanapi开关无线网卡--源码下载

//获取无线网卡开关状态
DWORD GetRadioState()
{
	DWORD dwError = ERROR_SUCCESS;
	HANDLE hClient = NULL;
	DWORD dwServiceVersion;
	GUID guidIntf;
	WLAN_RADIO_STATE wlanRadioState;
	PVOID pData = NULL;
	DWORD dwDataSize = 0;
	DWORD RadioState = -1;

	RPC_WSTR strGuid = EnumInterface();

	__try
	{
		// get the interface GUID
		if (UuidFromString(strGuid, &guidIntf) != RPC_S_OK)
		{
			dwError = ERROR_INVALID_PARAMETER;
			__leave;
		}

		// open handle
		if ((dwError = WlanOpenHandle(WLAN_API_VERSION, NULL, &dwServiceVersion, &hClient)) != ERROR_SUCCESS)
		{
			__leave;
		}

		// query radio state information
		// this opcode is not supported in XP
		if ((dwError = WlanQueryInterface(
			hClient,
			&guidIntf,
			wlan_intf_opcode_radio_state,
			NULL,                       // reserved
			&dwDataSize,
			&pData,
			NULL                        // not interesed in the type of the opcode value
			)) != ERROR_SUCCESS &&
			dwError != ERROR_NOT_SUPPORTED)
		{
			__leave;
		}

		if (dwError == ERROR_SUCCESS)
		{
			if (dwDataSize != sizeof(WLAN_RADIO_STATE))
			{
				dwError = ERROR_INVALID_DATA;
				__leave;
			}

			wlanRadioState = *((PWLAN_RADIO_STATE)pData);

			//wlanRadioState.PhyRadioState[0].dot11HardwareRadioState
			if (wlanRadioState.PhyRadioState[0].dot11SoftwareRadioState == dot11_radio_state_on)
			{
				RadioState = 1;
			}
			else
				if (wlanRadioState.PhyRadioState[0].dot11SoftwareRadioState == dot11_radio_state_off)
				{
					RadioState = 0;
				}
			WlanFreeMemory(pData);
			pData = NULL;
		}
		else
		{
			//wcout << L"Querying radio state is not supported." << endl;
		}

	}
	__finally
	{
		if (pData != NULL)
		{
			WlanFreeMemory(pData);
		}

		// clean up
		if (hClient != NULL)
		{
			WlanCloseHandle(
				hClient,
				NULL            // reserved
				);
		}
	}

	return RadioState;
}

//打开或关闭无线网卡
DWORD SetRadioState(int radiostate)
{
    DWORD dwError = ERROR_SUCCESS;
    HANDLE hClient = NULL;
	DWORD dwServiceVersion;
    GUID guidIntf;
    PWLAN_INTERFACE_CAPABILITY pInterfaceCapability = NULL;
    DWORD i;
    WLAN_PHY_RADIO_STATE wlanPhyRadioState;		

	if(radiostate==0)
	{
		wlanPhyRadioState.dot11SoftwareRadioState = dot11_radio_state_off;
	}else
	{
		wlanPhyRadioState.dot11SoftwareRadioState = dot11_radio_state_on;
	}

	RPC_WSTR strGuid = EnumInterface();
    __try
    { 
        // get the interface GUID
        if (UuidFromString(strGuid, &guidIntf) != RPC_S_OK)
        {
            dwError = ERROR_INVALID_PARAMETER;
            __leave;;
        }
		// open handle
		if ((dwError = WlanOpenHandle( WLAN_API_VERSION, NULL, &dwServiceVersion,&hClient)) != ERROR_SUCCESS)
		{
			__leave;
		}

        // get interface capability, which includes the supported PHYs
        if ((dwError = WlanGetInterfaceCapability(
                    hClient,
                    &guidIntf,
                    NULL,                       // reserved
                    &pInterfaceCapability
                    )) != ERROR_SUCCESS)
        {
            __leave;
        }

        // set radio state on every PHY
        for (i = 0; i < pInterfaceCapability->dwNumberOfSupportedPhys; i++)
        {
            // set radio state on every PHY
            wlanPhyRadioState.dwPhyIndex = i;

            if ((dwError = WlanSetInterface(
                            hClient, 
                            &guidIntf, 
                            wlan_intf_opcode_radio_state, 
                            sizeof(wlanPhyRadioState),
                            (PBYTE)&wlanPhyRadioState,
                            NULL                        // reserved
                            )) != ERROR_SUCCESS)
            {
                // rollback is nice to have, but not required
                __leave;
            }
        }

    }
    __finally
    {
        // clean up
        if (hClient != NULL)
        {
            WlanCloseHandle(hClient,NULL );
        }
        if (pInterfaceCapability != NULL)
        {
            WlanFreeMemory(pInterfaceCapability);
        }
    }   
	return dwError;
}


int _tmain(int argc, _TCHAR* argv[])
{
	if (GetRadioState() == 1)
	{
		SetRadioState(0);
	}
	else
	{
		SetRadioState(1);
	}
	return 0;
}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值