//获取无线网卡开关状态
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;
}