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

📄 devicemanagement.vb

📁 visual basic 2005 express 写的上位机
💻 VB
📖 第 1 页 / 共 2 页
字号:

                    '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)
                    Debug.WriteLine("cbsize = " & MyDeviceInterfaceDetailData.cbSize)

                    '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)


                    Debug.WriteLine(MyDebugging.ResultOfAPICall(" Result of second call: "))
                    Debug.WriteLine("  MyDeviceInterfaceDetailData.cbSize: " & CStr(MyDeviceInterfaceDetailData.cbSize))

                    '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.

                    SingledevicePathName = Marshal.PtrToStringAuto(pdevicePathName)
                    devicePathName(MemberIndex) = SingledevicePathName


                    Debug.WriteLine("Device Path = " & devicePathName(MemberIndex))
                    Debug.WriteLine("Device Path Length= " & Len(devicePathName(MemberIndex)))

                    'Free the memory allocated previously by AllocHGlobal.

                    Marshal.FreeHGlobal(DetailDataBuffer)
                    DeviceFound = True
                End If
                MemberIndex = MemberIndex + 1
            Loop Until (LastDevice = True)

            'Trim the array to the number of devices found.

            ReDim Preserve devicePathName(MemberIndex - 1)

            Debug.WriteLine("Number of HIDs found = " & MemberIndex - 1)

            '***
            'API function:
            'SetupDiDestroyDeviceInfoList

            'Purpose:
            'Frees the memory reserved for the DeviceInfoSet returned by SetupDiGetClassDevs.

            'Accepts:
            'A DeviceInfoSet returned by SetupDiGetClassDevs.

            'Returns:
            'True on success, False on failure.
            '***

            SetupDiDestroyDeviceInfoList _
                (DeviceInfoSet)

            Debug.WriteLine(MyDebugging.ResultOfAPICall("DestroyDeviceInfoList"))

            Return DeviceFound

        Catch ex As Exception
            Call HandleException(moduleName, ex)
        End Try

    End Function


    Friend Function RegisterForDeviceNotifications _
        (ByVal devicePathName As String, _
        ByVal formHandle As IntPtr, _
        ByVal classGuid As Guid, _
        ByRef deviceNotificationHandle As IntPtr) _
        As Boolean

        'Purpose    : Request to receive a notification when a device is attached or removed.

        'Accepts    : devicePathName - a handle to a device.
        '           : formHandle - a handle to the window that will receive device events.              
        '           : classGuid - an interface class GUID.  
        '             

        'Returns    : True on success, False on failure.

        '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: 
            'RegisterDeviceNotification

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

            'Accepts: 
            'Aa handle to the window that will receive device events
            'A pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of 
            'device to send notifications for,
            'DEVICE_NOTIFY_WINDOW_HANDLE to indicate that Handle is a window handle.

            'Returns:
            'A 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)

            'Find out if RegisterDeviceNotification was successful.

            If (deviceNotificationHandle.ToInt32 = IntPtr.Zero.ToInt32) Then
                Debug.WriteLine("RegisterDeviceNotification error")
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Call HandleException(ModuleName, ex)
        End Try
    End Function


    Friend Sub StopReceivingDeviceNotifications _
        (ByVal deviceNotificationHandle As IntPtr)

        'Purpose    : Requests to stop receiving notification messages when a device in an 
        '             interface class is attached or removed.

        'Accepts    : deviceNotificationHandle - a handle returned previously by
        '             RegisterDeviceNotification  

        Try

            '***
            'API function: UnregisterDeviceNotification

            'Purpose: Stop receiving notification messages.

            'Accepts: a handle returned previously by RegisterDeviceNotification  

            'Returns: True on success, False on failure.
            '***

            'Ignore failures.

            UnregisterDeviceNotification(deviceNotificationHandle)

        Catch ex As Exception
            Call HandleException(ModuleName, ex)
        End Try

    End Sub

    Shared Sub HandleException(ByVal moduleName As String, ByVal e As Exception)

        'Purpose    : Provides a central mechanism for exception handling.
        '           : Displays a message box that describes the exception.

        'Accepts    : moduleName - the module where the exception occurred.
        '           : e - the exception

        Dim Message As String
        Dim Caption As String

        Try
            'Create an error message.

            Message = "Exception: " & e.Message & ControlChars.CrLf & _
            "Module: " & ModuleName & ControlChars.CrLf & _
             "Method: " & e.TargetSite.Name

            'Specify a caption.

            Caption = "Unexpected Exception"

            'Display the message in a message box.

            MessageBox.Show(Message, Caption, MessageBoxButtons.OK)
            Debug.Write(Message)
        Finally
        End Try
    End Sub

End Class

⌨️ 快捷键说明

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