65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Mar 3, 2009

CPOL
viewsIcon

28622

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#

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

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.