このSetDevicePowerの第一引数に渡す値が端末により異なるので注意が必要です。
手元にある機種がSoftbankのX04HTとDocomoのX01Aだけなので、この2台で確認した結果です。
まず端末のレジストリ「HEKEY_LOCAL_MACHINE/System/CurrentControlSet/Control/Power/State」を確認します。
{98C5250D-C29A-4985-AE5F-AFE5367E5006}で始まるものが無線Lanになります。この値が1なら接続状態で0が切断状態です。
{98C5250D-C29A-4985-AE5F-AFE5367E5006}の後に続く¥以降の文字列が端末により異なります。
SoftbankのX04HTは{98C5250D-C29A-4985-AE5F-AFE5367E5006}¥TNETW12511
DocomoのX01Aは{98C5250D-C29A-4985-AE5F-AFE5367E5006}¥AR6K_SD1
この¥以降の文字列はレジストリ「HEKEY_LOCAL_MACHINE/Software/Drivers/WLAN」の「AdapterName」の値です。
以下のサンプルコードはSoftbankのX04HTとDocomoのX01Aで検証しましたが、他の端末で動作するかわかりません。
Imports System.Runtime.InteropServices
Public Class Form1
Private Enum DevicePowerState
Unspecified = -1
D0 = 0 ' Full On: full power, full functionality
D1 ' Low Power On: fully functional at low power/performance
D2 'Standby: partially powered with automatic wake
D3 'Sleep: partially powered with device initiated wake
D4 'Off: unpowered
End Enum
_
Private Shared Function SetDevicePower(ByVal pvDevice As String, ByVal dwDeviceFlags As Int32, ByVal deviceState As DevicePowerState) As Int32
End Function
_
Private Shared Function DevicePowerNotify(ByVal pvDevice As String, ByVal state As DevicePowerState, ByVal dwDeviceFlags As Int32) As Int32
End Function
Private Const POWER_NAME As Int32 = 1
Private Const wifiGUID As String = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}"
'WirelessLan ON
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
SetWiFi(True)
End Sub
'WirelessLan OFF
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
SetWiFi(False)
End Sub
Private Sub SetWiFi(ByVal isEnabled As Boolean)
'レジストリよりWiFiのドライバ名を取得する
Dim regDrivers As RegistryKey = Registry.LocalMachine.CreateSubKey("Software\Drivers\WLAN")
Dim driverNm As String = regDrivers.GetValue("AdapterName").ToString
Dim guid As String = wifiGUID & "¥" & driverNm
'D0でON、D4でOFF
If (isEnabled) Then
DevicePowerNotify(guid, DevicePowerState.D0, POWER_NAME)
SetDevicePower(guid, POWER_NAME, DevicePowerState.D0)
Else
DevicePowerNotify(guid, DevicePowerState.D4, POWER_NAME)
SetDevicePower(guid, POWER_NAME, DevicePowerState.D4)
End If
End Sub
End Class
0 件のコメント:
コメントを投稿