Click here to Skip to main content
15,867,308 members
Articles / Mobile Apps / Windows Mobile

Switch the wireless adapter state on, to low-power, and off on a Windows Mobile device

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Mar 2009CPOL 27.7K   26   4
Use PInvoke to control a wireless adapter's power status.

Introduction

This article uses a generic approach to set the power state of a wireless network adapter on a Windows Mobile device.

Using the code

Paste the code into a class, and call the static SetRadioPowerStatus method to set the radio to the state required.

The code in C#

C#
public enum DevicePowerState : int
{
    Unspecified = -1,
    FullPower = 0,      // Full On: full power, full functionality
    LowPower,           // Low Power On: fully functional at low power/performance
    Standby,            // Standby: partially powered with automatic wake
    Sleep,              // Sleep: partially powered with device initiated wake
    Off,                // Off: unpowered
}        

private const int POWER_NAME = 0x00000001;

[DllImport("coredll.dll", SetLastError = true)]
private static extern int DevicePowerNotify(string deviceId, 
                          DevicePowerState state, int flags);

[DllImport("coredll.dll", SetLastError = true)]
private static extern int SetDevicePower(string deviceId, 
                          int flags, DevicePowerState state);

public static void SetRadioPowerStatus(DevicePowerState state)
{
    String key = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\SWLD246L1";

    int i1 = DevicePowerNotify(key, state, POWER_NAME);

    if (i1 == 0)
    {
        int i2 = SetDevicePower(key, POWER_NAME, state);
    }
}

The code in VB.NET

VB
Public Enum DevicePowerState As Integer
    Unspecified = -1
    FullPower = 0       'Full On: full power, full functionality
    LowPower            'Low Power On: fully functional at low power/performance
    Standby             'Standby: partially powered with automatic wake
    Sleep               'Sleep: partially powered with device initiated wake
    Off                 'Off: unpowered
End Enum

Private Const POWER_NAME As Integer = &H1

Private Declare Function SetDevicePower Lib "coredll.dll" _
       (ByVal deviceId As String, ByVal flags As Integer, _
        ByVal state As DevicePowerState) As Integer
Private Declare Function DevicePowerNotify Lib "coredll.dll" _
       (ByVal deviceId As String, ByVal state As DevicePowerState, _
        ByVal flags As Integer) As Integer

Public Shared Sub SetRadioPowerStatus(ByVal state As DevicePowerState)
    Dim key As String = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}\SWLD246L1"

    Dim i1 As Integer = DevicePowerNotify(key, state, POWER_NAME)

    If i1 = 0 Then
        Dim i2 As Integer = SetDevicePower(key, POWER_NAME, state)
    End If
End Sub

Points of interest

Both P/Invoke calls must return 0 for success. Please note that the second part of the device name ("SWLD246L1", in this case) is dependant on your hardware. The name will be the name of one of the keys in the Registry under [HKLM]\Comms. Open each key in turn to find the one with a display name value equal to that of the installed adapter.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDevicePowerNotify returns 2 Pin
Tony Moffatt7-Aug-13 6:27
Tony Moffatt7-Aug-13 6:27 
QuestionError Pin
Member 100008801-May-13 23:52
Member 100008801-May-13 23:52 
GeneralGetting the device name Pin
Johan H. Roux5-Mar-09 1:58
Johan H. Roux5-Mar-09 1:58 
GeneralRe: Getting the device name Pin
coolbluesea5-Apr-10 21:42
coolbluesea5-Apr-10 21:42 
Yes, This way is fast. Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.