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, LowPower, Standby, Sleep, Off, }
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 LowPower Standby Sleep Off 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.