Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 3 Mar 2009
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

Johan H. Roux

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionDevicePowerNotify returns 2 PinmemberTony Moffatt7-Aug-13 6:27 
QuestionError PinmemberMember 100008801-May-13 23:52 
GeneralGetting the device name PinmemberJohan H. Roux5-Mar-09 1:58 
GeneralRe: Getting the device name Pinmembercoolbluesea5-Apr-10 21:42 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 3 Mar 2009
Article Copyright 2009 by Johan H. Roux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid