Wince6.0 wifi on/off Control

Source: Internet
Author: User

# 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_pai_read | file_pai_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 management
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 );
}

Add the required files to the sources file:
Wzcsapi. Lib
Iphlpapi. Lib


Call:
Adapterpoweronoff (szadapter1, 1); // open
Adapterpoweronoff (szadapter1, 0); // close
It is simple. After it is disabled, the closed adapter cannot be found in the system. It is very clean! However, the premise is that the WiFi interface driver (My hsmmc0) must be able to work normally, my hsmmc0 is because ch0 does not detect the foot, which leads to the insertion and removal of the Wi-Fi module.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.