[置顶] WINCE6.0 禁用和启用WIFI代码

        下面的代码实现如何在WINCE 6.0 下面禁用和启用WIFI,效果相当于在控制面板的网络连接手工禁用和启用,本人测试的WIFI是GH381,SDIO86881

 禁用WIFI代码

     

int __stdcall DisableWifi()
{
	HANDLE                    hNdisPwr;
	NDISPWR_SAVEPOWERSTATE    SavePowerState;
	TCHAR       multiSz[257];
	DWORD       cbBuffer = sizeof(multiSz);

	StringCchCopy(multiSz, (cbBuffer / sizeof(TCHAR))-2, _T("SDIO86881"));

	hNdisPwr = CreateFile(
		(PTCHAR)NDISPWR_DEVICE_NAME,					//	Object name.
		0x00,											//	Desired access.
		0x00,											//	Share Mode.
		NULL,											//	Security Attr
		OPEN_EXISTING,									//	Creation Disposition.
		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,	//	Flag and Attributes..
		(HANDLE)INVALID_HANDLE_VALUE);	

	if (hNdisPwr != INVALID_HANDLE_VALUE)
	{
		SavePowerState.pwcAdapterName = _T("SDIO86881");

		//
		//  If it is currently enabled, set the power level to D4.
		//
		SavePowerState.CePowerState   = D4;

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

		CloseHandle(hNdisPwr);
		if(result!=TRUE)
			return -1;

	}	   
	else
	{
		RETAILMSG (1,   
			(TEXT("hNdisPwr is INVALID_HANDLE_VALUE\r\n")));
		return -1;
	}
	SetDevicePower(_T("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\SDIO86881"), POWER_NAME, D4);


	multiSz[::_tcslen(multiSz)+1] = _T('\0'); // Multi sz needs an extra null

	BOOL result1=DoNdisIOControl(
		IOCTL_NDIS_UNBIND_ADAPTER, 
		multiSz,
		(_tcslen(multiSz)+2) * sizeof(TCHAR),
		NULL, 
		NULL);  
    if(result1!=TRUE)
		 return -1;

	return 0;

}

启用WIFI

int __stdcall EnableWifi()
{
	HANDLE                    hNdisPwr;
	NDISPWR_SAVEPOWERSTATE    SavePowerState;
	TCHAR       multiSz[257];
	DWORD       cbBuffer = sizeof(multiSz);


	StringCchCopy(multiSz, (cbBuffer / sizeof(TCHAR))-2, _T("SDIO86881"));


	hNdisPwr = CreateFile(
		(PTCHAR)NDISPWR_DEVICE_NAME,					//	Object name.
		0x00,											//	Desired access.
		0x00,											//	Share Mode.
		NULL,											//	Security Attr
		OPEN_EXISTING,									//	Creation Disposition.
		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,	//	Flag and Attributes..
		(HANDLE)INVALID_HANDLE_VALUE);	


	if (hNdisPwr != INVALID_HANDLE_VALUE)
	{
		SavePowerState.pwcAdapterName = _T("SDIO86881");


		//
		//  If it is currently enabled, set the power level to D4.
		//
		SavePowerState.CePowerState   = PwrDeviceUnspecified;


		BOOL result=DeviceIoControl(
			hNdisPwr,
			IOCTL_NPW_SAVE_POWER_STATE,
			&SavePowerState,
			sizeof(NDISPWR_SAVEPOWERSTATE),
			NULL,
			0x00,
			NULL,
			NULL);    
        
		CloseHandle(hNdisPwr);
		if(result!=TRUE)
			return -1;


	}	   
	else
	{
		return -1;
	}
	SetDevicePower(_T("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\SDIO86881"), POWER_NAME, PwrDeviceUnspecified);




	multiSz[::_tcslen(multiSz)+1] = _T('\0'); // Multi sz needs an extra null


	BOOL result1=DoNdisIOControl(
		IOCTL_NDIS_BIND_ADAPTER, 
		multiSz,
		(_tcslen(multiSz)+2) * sizeof(TCHAR),
		NULL, 
		NULL);  
	if(result1!=TRUE)
		return -1;
	return 0;
}


你可能感兴趣的:([置顶] WINCE6.0 禁用和启用WIFI代码)