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

📄 devicemanagement.vb

📁 visual basic 2005 express 写的上位机
💻 VB
📖 第 1 页 / 共 2 页
字号:
Option Strict On
Option Explicit On 
Imports System.Runtime.InteropServices

Friend Class DeviceManagement

    'For detecting devices and receiving device notifications.

    'Used in error messages:

    Const moduleName As String = "DeviceManagement"

    'For viewing results of API calls in debug.write statements:

    Dim MyDebugging As New Debugging()


    Friend Function DeviceNameMatch _
        (ByVal m As Message, _
        ByVal mydevicePathName As String) _
        As Boolean

        'Purpose    : Compares two device path names. Used to find out if the device name 
        '           : of a recently attached or removed device matches the name of a 
        '           : device the application is communicating with.

        'Accepts    : m - a WM_DEVICECHANGE message. A call to RegisterDeviceNotification
        '           : causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.
        '           : mydevicePathName - a device pathname returned by SetupDiGetDeviceInterfaceDetail
        '           : in an SP_DEVICE_INTERFACE_DETAIL_DATA structure.              

        'Returns    : True if the names match, False if not.

        Try
            Dim DevBroadcastDeviceInterface As New DEV_BROADCAST_DEVICEINTERFACE_1()
            Dim DevBroadcastHeader As New DEV_BROADCAST_HDR()

            'The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure.

            Marshal.PtrToStructure(m.LParam, DevBroadcastHeader)

            If (DevBroadcastHeader.dbch_devicetype = DBT_DEVTYP_DEVICEINTERFACE) Then

                'The dbch_devicetype parameter indicates that the event applies to a device interface.
                'So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure, 
                'which begins with a DEV_BROADCAST_HDR.

                'Obtain the number of characters in dbch_name by subtracting the 32 bytes
                'in the strucutre that are not part of dbch_name and dividing by 2 because there are 
                '2 bytes per character.

                Dim StringSize As Integer = CInt((DevBroadcastHeader.dbch_size - 32) / 2)

                'The dbcc_name parameter of DevBroadcastDeviceInterface contains the device name. 
                'Trim dbcc_name to match the size of the string.

                ReDim DevBroadcastDeviceInterface.dbcc_name(StringSize)

                'Marshal data from the unmanaged block pointed to by m.LParam 
                'to the managed object DevBroadcastDeviceInterface.

                Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface)

                'Store the device name in a String.

                Dim DeviceNameString As New String(DevBroadcastDeviceInterface.dbcc_name, 0, StringSize)

                Debug.WriteLine("Device Name =      " & DeviceNameString)
                Debug.WriteLine("")
                Debug.WriteLine("myDevicePathName = " & mydevicePathName)
                Debug.WriteLine("")

                'Compare the name of the newly attached device with the name of the device 
                'the application is accessing (mydevicePathName).
                'Set ignorecase True.

                If (String.Compare(DeviceNameString, mydevicePathName, True) = 0) Then

                    'The name matches.

                    Return True
                Else

                    'It's a different device.

                    Return False
                End If
            End If

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

    End Function


    Friend Function FindDeviceFromGuid _
        (ByVal myGuid As System.Guid, _
        ByRef devicePathName() As String) _
        As Boolean

        'Purpose    : Uses SetupDi API functions to retrieve the device path name of an
        '           : attached device that belongs to an interface class.

        'Accepts    : myGuid - an interface class GUID.
        '           : devicePathName - a pointer to an array of strings that will contain 
        '           : the device path names of attached devices.  

        'Returns    : True if at least one device is found, False if not. 

        Dim DeviceFound As Boolean
        Dim DeviceInfoSet As IntPtr
        Dim LastDevice As Boolean
        Dim BufferSize As Integer
        Dim MemberIndex As Integer
        Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA
        Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
        Dim Result As Boolean
        Dim SingledevicePathName As String
        Dim Success As Boolean

        Try
            '***
            'API function: SetupDiGetClassDevs

            'Purpose: 
            'Retrieves a device information set for a specified group of devices.
            'SetupDiEnumDeviceInterfaces uses the device information set.

            'Accepts: 
            'An interface class GUID
            'Null to retrieve information for all device instances
            'An optional handle to a top-level window (unused here)
            'Flags to limit the returned information to currently present devices 
            'and devices that expose interfaces in the class specified by the GUID.

            'Returns:
            'A handle to a device information set for the devices.
            '***

            DeviceInfoSet = SetupDiGetClassDevs _
                (myGuid, _
                vbNullString, _
                0, _
                DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)

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

            DeviceFound = False
            MemberIndex = 0

            Do

                'Begin with 0 and increment through the device information set until
                'no more devices are available.

                'The cbSize element of the MyDeviceInterfaceData structure must be set to
                'the structure's size in bytes. The size is 28 bytes.
                MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData)

                '***
                'API function: 
                'SetupDiEnumDeviceInterfaces()

                'Purpose: Retrieves a handle to a SP_DEVICE_INTERFACE_DATA 
                'structure for a device.
                'On return, MyDeviceInterfaceData contains the handle to a
                'SP_DEVICE_INTERFACE_DATA structure for a detected device.

                'Accepts:
                'A DeviceInfoSet returned by SetupDiGetClassDevs.
                'An interface class GUID.
                'An index to specify a device in a device information set.
                'A pointer to a handle to a SP_DEVICE_INTERFACE_DATA structure for a device.

                'Returns:
                'Non-zero on success, zero on True.
                '***

                Result = SetupDiEnumDeviceInterfaces _
                    (DeviceInfoSet, _
                    0, _
                    myGuid, _
                    MemberIndex, _
                    MyDeviceInterfaceData)

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

                'Find out if a device information set was retrieved.

                If (Result = False) Then
                    LastDevice = True

                Else
                    'A device is present.

                    Debug.WriteLine("  DeviceInfoSet for device #" & CStr(MemberIndex) & ": ")
                    Debug.WriteLine("  cbSize = " & CStr(MyDeviceInterfaceData.cbSize))
                    Debug.WriteLine("  InterfaceclassGuid = " & MyDeviceInterfaceData.InterfaceClassGuid.ToString)
                    Debug.WriteLine("  Flags = " & Hex(MyDeviceInterfaceData.Flags))

                    '***
                    'API function: 
                    'SetupDiGetDeviceInterfaceDetail()

                    'Purpose:
                    'Retrieves an SP_DEVICE_INTERFACE_DETAIL_DATA structure
                    'containing information about a device.
                    'To retrieve the information, call this function twice.
                    'The first time returns the size of the structure.
                    'The second time returns a pointer to the data.

                    'Accepts:
                    'A DeviceInfoSet returned by SetupDiGetClassDevs
                    'An SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces
                    'A pointer to an SP_DEVICE_INTERFACE_DETAIL_DATA structure to receive information 
                    'about the specified interface.
                    'The size of the SP_DEVICE_INTERFACE_DETAIL_DATA structure.
                    'A pointer to a variable that will receive the returned required size of the 
                    'SP_DEVICE_INTERFACE_DETAIL_DATA structure.
                    'A pointer to an SP_DEVINFO_DATA structure to receive information about the device.

                    'Returns:
                    'Non-zero on success, zero on failure.
                    '***

                    MyDeviceInterfaceDetailData = Nothing

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

                    Debug.WriteLine(MyDebugging.ResultOfAPICall("SetupDiGetDeviceInterfaceDetail"))
                    Debug.WriteLine("  (OK to say too small)")
                    Debug.WriteLine("  Required buffer size for the data: " & BufferSize)

                    'Store the structure's size.

                    MyDeviceInterfaceDetailData.cbSize = Marshal.SizeOf(MyDeviceInterfaceDetailData)

⌨️ 快捷键说明

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