⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicemanagement.vb

📁 microsoft winusb interface for vb
💻 VB
📖 第 1 页 / 共 2 页
字号:
                        MyDeviceInterfaceData, _
                        IntPtr.Zero, _
                        0, _
                        BufferSize, _
                        IntPtr.Zero)

                    'Store the structure's size.

                    MyDeviceInterfaceDetailData.cbSize = Marshal.SizeOf(MyDeviceInterfaceDetailData)

                    'Allocate memory for the MyDeviceInterfaceDetailData Structure using the returned buffer size.

                    Dim DetailDataBuffer As IntPtr = Marshal.AllocHGlobal(BufferSize)

                    'Store cbSize in the first 4 bytes of the array

                    Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize)

                    'Call SetupDiGetDeviceInterfaceDetail again.
                    'This time, pass a pointer to DetailDataBuffer
                    'and the returned required buffer size.

                    Success = SetupDiGetDeviceInterfaceDetail _
                        (DeviceInfoSet, _
                        MyDeviceInterfaceData, _
                        DetailDataBuffer, _
                        BufferSize, _
                        BufferSize, _
                        IntPtr.Zero)

                    'Skip over cbsize (4 bytes) to get the address of the devicePathName.

                    Dim pdevicePathName As IntPtr = New IntPtr(DetailDataBuffer.ToInt32 + 4)

                    'Get the String containing the devicePathName.

                    devicePathName = Marshal.PtrToStringAuto(pdevicePathName)

                    'Free the memory allocated previously by AllocHGlobal.

                    Marshal.FreeHGlobal(DetailDataBuffer)
                    DeviceFound = True

                End If

                MemberIndex = MemberIndex + 1

            Loop Until (LastDevice = True)

            '***
            ' API function

            ' summary
            ' Frees the memory reserved for the DeviceInfoSet returned by SetupDiGetClassDevs.

            ' parameters
            ' DeviceInfoSet returned by SetupDiGetClassDevs.

            ' returns
            ' True on success.
            '***

            SetupDiDestroyDeviceInfoList _
                (DeviceInfoSet)

            Return DeviceFound

        Catch ex As Exception
            Throw
        End Try

    End Function

    ''' <summary>
    ''' Requests to receive a notification when a device is attached or removed.
    ''' </summary>
    ''' 
    ''' <param name="devicePathName"> handle to a device. </param>
    ''' <param name="formHandle"> handle to the window that will receive device events. </param>
    ''' <param name="classGuid"> device interface GUID. </param>
    ''' <param name="deviceNotificationHandle"> returned device notification handle. </param>
    ''' 
    ''' <returns>
    ''' True on success.
    ''' </returns>
    ''' 
    Friend Function RegisterForDeviceNotifications _
        (ByVal devicePathName As String, _
        ByVal formHandle As IntPtr, _
        ByVal classGuid As Guid, _
        ByRef deviceNotificationHandle As IntPtr) _
        As Boolean

        'A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.

        Dim DevBroadcastDeviceInterface As DEV_BROADCAST_DEVICEINTERFACE = _
            New DEV_BROADCAST_DEVICEINTERFACE()
        Dim DevBroadcastDeviceInterfaceBuffer As IntPtr

        Dim size As Integer

        Try
            'Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.

            'Set the size.

            size = Marshal.SizeOf(DevBroadcastDeviceInterface)
            DevBroadcastDeviceInterface.dbcc_size = size

            'Request to receive notifications about a class of devices.

            DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE

            DevBroadcastDeviceInterface.dbcc_reserved = 0

            'Specify the interface class to receive notifications about.

            DevBroadcastDeviceInterface.dbcc_classguid = classGuid

            'Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.

            DevBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size)

            'Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
            'Set fDeleteOld True to prevent memory leaks.

            Marshal.StructureToPtr _
                (DevBroadcastDeviceInterface, DevBroadcastDeviceInterfaceBuffer, True)

            '***
            ' API function

            ' summary
            ' Request to receive notification messages when a device in an interface class
            ' is attached or removed.

            ' parameters 
            ' Handle to the window that will receive device events.
            ' Pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of 
            ' device to send notifications for.
            ' DEVICE_NOTIFY_WINDOW_HANDLE indicates the handle is a window handle.

            ' Returns
            ' Device notification handle or NULL on failure.
            '***

            deviceNotificationHandle = RegisterDeviceNotification _
                (formHandle, _
                DevBroadcastDeviceInterfaceBuffer, _
                DEVICE_NOTIFY_WINDOW_HANDLE)

            'Marshal data from the unmanaged block DevBroadcastDeviceInterfaceBuffer to
            'the managed object DevBroadcastDeviceInterface

            Marshal.PtrToStructure(DevBroadcastDeviceInterfaceBuffer, DevBroadcastDeviceInterface)

            'Free the memory allocated previously by AllocHGlobal.

            Marshal.FreeHGlobal(DevBroadcastDeviceInterfaceBuffer)

            If (deviceNotificationHandle.ToInt32 = IntPtr.Zero.ToInt32) Then
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Throw
        End Try
    End Function

    ''' <summary>
    ''' Requests to stop receiving notification messages when a device in an
    ''' interface class is attached or removed.
    ''' </summary>
    ''' 
    ''' <param name="deviceNotificationHandle"> handle returned previously by
    ''' RegisterDeviceNotification. </param>

    Friend Sub StopReceivingDeviceNotifications _
        (ByVal deviceNotificationHandle As IntPtr)

        Try

            '***
            ' API function

            ' summary
            ' Stop receiving notification messages.

            ' parameters
            ' Handle returned previously by RegisterDeviceNotification.  

            ' returns
            ' True on success.
            '***

            ' Ignore failures.

            UnregisterDeviceNotification(deviceNotificationHandle)

        Catch ex As Exception
            Throw
        End Try

    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -