タイトル | : Re: プラグアンドプレイでポートの開放、切断をしたい |
記事No | : 14739 |
投稿日 | : 2010/06/03(Thu) 20:05 |
投稿者 | : K.J.K. |
Windows 2000 なので確認ができませんが、とりあえず、SysInfo.ocx を 使い続けるのであれば、こういう感じかも。
Private Type UUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0& To 7&) As Byte End Type
Private Type DeviceBroadcastInterfaceWithoutName Size As Long DeviceType As Long Reserved As Long GUID As UUID DeviceName(0& To 3&) As Byte End Type
Private Declare Sub RtlMoveMemory Lib "Kernel32.dll" _ (ByRef Dest As Any, ByRef Src As Any, _ Optional ByVal Length As Long = 4&)
Private Declare Function RegisterDeviceNotification _ Lib "User32.dll" Alias "RegisterDeviceNotificationA" _ (ByVal hRecipient As OLE_HANDLE, _ ByRef NotificationFilter As Any, _ Optional ByVal Flags As Long = 0&) As OLE_HANDLE ' Flags = 0& : DEVICE_NOTIFY_WINDOW_HANDLE
Private Declare Function UnregisterDeviceNotification Lib "User32.dll" _ (ByVal hDeviceNotification As OLE_HANDLE) As Long
Private mhDeviceNotification As OLE_HANDLE
Private Function SysInfoRegisterForUSB(ByVal SysInfoControl As SysInfo) As OLE_HANDLE Dim uFilter As DeviceBroadcastInterfaceWithoutName Dim hWndSysInfo As OLE_HANDLE Dim fVersion As Single Dim iFlag As Long
If SysInfoControl Is Nothing Then Exit Function
fVersion = SysInfoControl.OSVersion
With uFilter .Size = LenB(uFilter) .DeviceType = &H5& ' DBT_DEVTYP_DEVICEINTERFACE End With
If fVersion < 5.01 Then ' GUID_DEVINTERFACE_USB_DEVICE With uFilter.GUID .Data1 = &HA5DCBF10 .Data2 = &H6530 .Data3 = &H11D2 .Data4(0&) = &H90 .Data4(1&) = &H1F .Data4(2&) = &H0 .Data4(3&) = &HC0 .Data4(4&) = &H4F .Data4(5&) = &HB9 .Data4(6&) = &H51 .Data4(7&) = &HED End With iFlag = 0& Else iFlag = 4& End If ' Offset = 208&
Call RtlMoveMemory(hWndSysInfo, ByVal (ObjPtr(SysInfoControl.Object) + 208&)) SysInfoRegisterForUSB = RegisterDeviceNotification(hWndSysInfo, uFilter, iFlag) End Function
Private Function SysInfoUnregister(ByRef hDeviceNotification As OLE_HANDLE) As Boolean If 0& = hDeviceNotification Then SysInfoUnregister = True Exit Function End If If UnregisterDeviceNotification(hDeviceNotification) Then hDeviceNotification = 0& SysInfoUnregister = True End If End Function
' SysInfo コントロールの名前は moSysInfo であると仮定しています。 Private Sub Form_Load() mhDeviceNotification = SysInfoRegisterForUSB(moSysInfo) End Sub
Private Sub Form_Unload(Cancel As Integer) Call SysInfoUnregister(mhDeviceNotification) End Sub
|