Windows Mobile 5Windows Mobile 6硬件Windows MobileVisual Basic 9 (2008)Visual Basic 8 (2005)C# 2.0C# 3.0中级开发Visual BasicC#
在 Windows Mobile 设备上切换无线适配器的状态:开启、低功耗和关闭





5.00/5 (2投票s)
使用 PInvoke 控制无线适配器的电源状态。
引言
本文使用一种通用的方法来设置 Windows Mobile 设备上无线网络适配器的电源状态。
使用代码
将代码粘贴到类中,并调用静态方法 SetRadioPowerStatus
来将无线电设置为所需的状态。
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);
}
}
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
关注点
两个 P/Invoke 调用都必须返回 0 才能成功。请注意,设备名称的第二部分(在本例中为“SWLD246L1”)取决于您的硬件。名称将是注册表 [HKLM]\Comms 下的一个键的值。依次打开每个键,找到显示名称值等于已安装适配器名称的键。