Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

USB and Network - Attach/Detach Device

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • USB and Network - Attach/Detach Device

    For use of finding info on a device attached or detached (USB or Network Drive)

    To test, Unplug or Re-Seat a USB device, or map a network drive and see the results.

    Hopefully just one of many examples using USB, but till then Have FUN

    DeviceNotify.inc
    Code:
    '*************************************************************************************************************
    '*** Project:
    '***      RegisterDeviceInterface
    '*** Description:
    '***      Wrapper for watching USB
    '*** Programmer:
    '***      Cliff Nichols
    '*** Based from:
    '***      www.lvr.com
    '***      www.powerbasic.com
    '***      www.msdn.com
    '*** Post Date:
    '***      12-14-07
    '*** Last Update:
    '***      01-22-09
    '*** Changes, Updates, Patches, etc....
    '***      Complete Re-Write from previous attempts to understand USB concepts
    '***
    '*** OS Versions:
    '***      Should work on 95/98/ME, NT/2000/XP/VISTA
    '*** Tested On:
    '***      XP/VISTA
    '*** PB Versions:
    '***      7, 8, 9
    '*** Based From:
    '***      Prior versions, Documentation, and Help from others in PB
    '*** Special Thanks:
    '***      Kev Peel for SetupApi.INC
    '***      PierreBellisle for DeviceNotify.bas
    '***      Walt Decker, José Roca, Knuth Konrad and others for their reminders of concepts that
    '***            are often overlooked (see "Parameter as Reply" discussion for more)
    '*************************************************************************************************************
         'The complete list
         'Device = "1394/1394debug/61883/adapter/apmsupport/avc/battery/biometric/" & _
         '         "bluetooth/cdrom/computer/decoder/diskdrive/display/"            & _
         '         "dot4print/enum1394/fdc/floppydisk/gps/hdc/hidclass/image/"      & _
         '         "infrared/keyboard/legacydriver/media/mediumchanger/mtd/modem/"  & _
         '         "monitor/mouse/multifunction/multiportserial/net/netclient/"     & _
         '         "netservice/nettrans/nodriver/pcmcia/ports/printer/"             & _
         '         "printer upgrade/pnpprinters/processor/sbp2/scsiadapter/"        & _
         '         "security accelerator/smartcardreader/sound/system/tapedrive/"   & _
         '         "unknown/usb/volume/volumesnapshot/wceusbs"
    
    #IF NOT %DEF(%DEVICENOTIFY)
         %DEVICENOTIFY = 1
    '*** %WM_DEVICECHANGE/CBWPARAM specifies the event/CBLPARAM specifies data
    '***
         %WM_DEVICECHANGE                        = &H219&                                     '537 - Notifies application of change to hardware configuration of device or computer. (received through WindowProc)
    'wParam - One of the following
         %DBT_CONFIGCHANGECANCELED               = &H19&                                      '25 - Request to change the current configuration (dock or undock) has been canceled
         %DBT_CONFIGCHANGED                      = &H18&                                      '24 - Current configuration changed, due to a dock or undock
         %DBT_CUSTOMEVENT                        = &H8006&                                    '32774 - Custom event
         %DBT_DEVICEARRIVAL                      = &H8000&                                    '32768 - Device or piece of media has been inserted and is now available
         %DBT_DEVICEQUERYREMOVE                  = &H8001&                                    '32769 - Permission requested to remove Device or media. (Any application can deny this request and cancel the removal.)
         %DBT_DEVICEQUERYREMOVEFAILED            = &H8002&                                    '32770 - Request to remove device or media canceled.
         %DBT_DEVICEREMOVECOMPLETE               = &H8004&                                    '32772 - Device or Media removed.
         %DBT_DEVICEREMOVEPENDING                = &H8003&                                    '32771 - Device or Media about to be removed. (Cannot be denied)
         %DBT_DEVICETYPESPECIFIC                 = &H8005&                                    '32773 - Device-specific event
         %DBT_DEVNODES_CHANGED                   = &H7&                                       '7 - Device added or removed from the system.
         %DBT_QUERYCHANGECONFIG                  = &H17&                                      '23 - Permission requested to change current configuration (dock or undock)
         %DBT_USERDEFINED                        = &HFFFF&                                    '65535 - User-Defined
    'lParam - Pointer to structure that contains event-specific data. (Format depends on wParam)
    
    
    'Drive or media, networked or not
         %DBTF_MEDIA                             = 1                                          'Media comings and goings
         %DBTF_NET                               = 2                                          'Network volume
    
         %DEVICE_NOTIFY_ALL_INTERFACE_CLASSES    = &H4&                                       'All Interfaces
         %DEVICE_NOTIFY_WINDOW_HANDLE            = 0                                          'Notifications sent using WM_POWERBROADCAST with wParam = PBT_POWERSETTINGCHANGE.
         %DEVICE_NOTIFY_SERVICE_HANDLE           = 1                                          'Notifications sent to HandlerEx callback function with dwControl = SERVICE_CONTROL_POWEREVENT and dwEventType = PBT_POWERSETTINGCHANGE.
         %GWL_WNDPROC                            = (-4)                                       'Sets new address for the window procedure. (Windows NT/2000/XP: Can not change attribute if window does not belong to same process as alling thread.)
         %UNSAFE_REMOVE                          = &H1C&                                      'Unsafe removal of device
    
    'DEV_BROADCAST_HDR Structure
         %DBT_DEVTYP_OEM                         = &H0&                                       '0 - Original Equipment Manufacturer(OEM) or Independent Hardware Vendor(IHV) defined device type. This structure is a DEV_BROADCAST_OEM structure.
         %DBT_DEVTYP_DEVNODE                     = &H1&                                       '1 - Devnode number (specific to Windows 95)
         %DBT_DEVTYP_VOLUME                      = &H2&                                       '2 - Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.
         %DBT_DEVTYP_PORT                        = &H3&                                       '3 - Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT structure.
         %DBT_DEVTYP_NET                         = &H4&                                       '4 - Network Resource (UNC)
         %DBT_DEVTYP_DEVICEINTERFACE             = &H5&                                       '5 - Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
         %DBT_DEVTYP_HANDLE                      = &H6&                                       '6 - File system handle. This structure is a DEV_BROADCAST_HANDLE structure.
    
    '*** EVENT HEADER
         TYPE DEV_BROADCAST_HDR
              DbhSize        AS DWORD                                                         'Size of Structure (If User-Defined event, this must be size of header, plus size of variable-length data in _DEV_BROADCAST_USERDEFINED structure.
              DbhDeviceType  AS DWORD                                                         'Device Type (Could be Interface/Handle/OEM/Port/Volume)
              DbhReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
         END TYPE
    
    '*** DEVICE TYPE INFO
         TYPE DEV_BROADCAST_OEM
              DboSize        AS DWORD                                                         'Size of Structure
              DboDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYPE_OEM.
              DboReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DboIdentifier  AS DWORD                                                         'OEM-specific identifier for the device.
              DboSuppfunc    AS DWORD                                                         'OEM-specific function value. Possible values depend on the device.
         END TYPE
    
         TYPE DEV_BROADCAST_DEVNODE
              DbnSize        AS DWORD                                                         'Size of Structure
              DbnDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYP_DEVNODE
              DbnReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbnDevNode     AS DWORD                                                         'OEM-specific identifier for the device.
         END TYPE
    
         TYPE DEV_BROADCAST_VOLUME
              Dbvsize        AS DWORD                                                         'Size of Structure
              DbvDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYP_VOLUME.
              DbvReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbvUnitmask    AS DWORD                                                         'Logical drive, bit 0 is A:, bit 1 is B:...
              DbvFlags       AS WORD                                                          'If %DBTF_MEDIA (Change affects media in drive). If Not %DBTF_MEDIA (Change affects physical device or drive.), If %DBTF_NET = network volume.
         END TYPE
    
         TYPE DEV_BROADCAST_PORT
              DbpSize        AS DWORD                                                         'Size of Structure
              DbpDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYP_PORT                '<--- Set to check if a port device (see DEV_BROADCAST_DEVICEINTERFACE to find interface)
              DbpReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbpName        AS ASCIIZ * %MAX_PATH                                            'Device 'Friendly' Name such as "Com1" and "Standard 28800 bps Modem"
         END TYPE
    
         TYPE DEV_BROADCAST_NET
              DbnSize        AS DWORD                                                         'Size of Structure
              DbnDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYP_NET                '<--- Set to check if a port device (see DEV_BROADCAST_DEVICEINTERFACE to find interface)
              DbnReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbnName        AS ASCIIZ * %MAX_PATH                                            'Device 'Friendly' Name such as "Com1" and "Standard 28800 bps Modem"
         END TYPE
    
         TYPE DEV_BROADCAST_DEVICEINTERFACE
              DbDiSize       AS DWORD                                                         'Size of Structure
              DbDiDeviceType AS DWORD                                                         'Set to %DBT_DEVTYP_DEVICEINTERFACE.    '<--- Set to get Interface Type
              DbDiReserved   AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbDiClassguid  AS GUID                                                          'GUID for Interface Device Class
              DbDiName       AS ASCIIZ * %MAX_PATH                                            'When returned through %WM_DEVICECHANGE message, converted to ANSI as appropriate.
                                                                                              'Services always receive a Unicode string, both (RegisterDeviceNotificationW or RegisterDeviceNotificationA)
         END TYPE
    
         TYPE DEV_BROADCAST_HANDLE
              DbhSize        AS DWORD                                                         'Size of Structure
              DbhDeviceType  AS DWORD                                                         'Set to %DBT_DEVTYP_HANDLE
              DbhReserved    AS DWORD                                                         'Reserved; do not use.                  '<--- No idea why, but its what the doc says
              DbhHandle      AS DWORD                                                         'Handle in RegisterDeviceNotification function.
              DbhHdevNotify  AS DWORD                                                         'Handle to device notification, returned by the RegisterDeviceNotification.
    '*** The following are valid only for DBT_CUSTOMEVENT
              DbhEventGuid   AS GUID                                                          'Custom event GUID
              DbhNameOffset  AS LONG                                                          'The offset to event name.
              DbhData(0)     AS BYTE                                                          'Optional binary data.
         END TYPE
    
         GLOBAL glngPrevDeviceNotifyProc AS LONG                                              'Original Proc to be notified
         GLOBAL hDevNotify AS LONG                                                            'Handle to RegisterDeviceNotification function
    
    DECLARE SUB AddToList (BYVAL LbString AS STRING)
    DECLARE FUNCTION GetUdtDeviceEvent( _
                                  DeviceBroadCastHeader AS DEV_BROADCAST_HDR, _
                                  DeviceBroadCastOem AS DEV_BROADCAST_OEM, _
                                  DeviceBroadCasteDevNode AS DEV_BROADCAST_DEVNODE, _
                                  DeviceBroadCastVolume AS DEV_BROADCAST_VOLUME, _
                                  DeviceBroadCastPort AS DEV_BROADCAST_PORT, _
                                  DeviceBroadCastNet AS DEV_BROADCAST_NET, _
                                  DeviceBroadCastDeviceInterface AS DEV_BROADCAST_DEVICEINTERFACE, _
                                  DeviceBroadCastHandle AS DEV_BROADCAST_HANDLE, _
                                  BYVAL lParam AS LONG _
                             )AS STRING       
    FUNCTION DeviceNotifyProc(BYVAL hWnd AS LONG, BYVAL Msg AS LONG, BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
         STATIC Message AS STRING
         LOCAL DeviceBroadCastHeader AS DEV_BROADCAST_HDR
         LOCAL DeviceBroadCastOem AS DEV_BROADCAST_OEM
         LOCAL DeviceBroadCasteDevNode AS DEV_BROADCAST_DEVNODE
         LOCAL DeviceBroadCastVolume AS DEV_BROADCAST_VOLUME
         LOCAL DeviceBroadCastPort AS DEV_BROADCAST_PORT
         LOCAL DeviceBroadCastNet AS DEV_BROADCAST_NET
         LOCAL DeviceBroadCastDeviceInterface AS DEV_BROADCAST_DEVICEINTERFACE
         LOCAL DeviceBroadCastHandle AS DEV_BROADCAST_HANDLE
    
    
         LOCAL TempBin AS STRING
         LOCAL i AS LONG
         LOCAL DriveName AS STRING
         LOCAL DriveFlags AS STRING
         STATIC DevNodesChanged AS LONG
         SELECT CASE Msg
              CASE %WM_DEVICECHANGE
                   Message = Message + SPACE$(5) + "WM_DEVICECHANGE "
                   SELECT CASE wParam                                                         'Determine Event Type
                        CASE %DBT_CONFIGCHANGECANCELED                                        'System uses WM_DEVICECHANGE, wParam = DBT_CONFIGCHANGECANCELED, lParam = 0
                             Message = Message + SPACE$(5) + "wParam = DBT_CONFIGCHANGECANCELED"
                        CASE %DBT_CONFIGCHANGED                                               'System uses WM_DEVICECHANGE, wParam = DBT_CONFIGCHANGED, lParam = 0
                             Message = Message + SPACE$(5) + "wParam = DBT_CONFIGCHANGED"
                        CASE %DBT_CUSTOMEVENT                                                 'System uses WM_DEVICECHANGE, wParam = DBT_CUSTOMEVENT, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_CUSTOMEVENT"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVICEARRIVAL                                               'System uses WM_DEVICECHANGE, wParam = DBT_DEVICEARRIVAL, lParam = TBD
                             SELECT CASE DevNodesChanged
                                  CASE %FALSE         'Flag not set so set it
                                       DevNodesChanged = %TRUE
                                  CASE %TRUE          'Flag set
                             END SELECT
    '                         ZeroMemory VARPTR(DeviceBroadCastHeader), SIZEOF(DeviceBroadCastHeader)
    '                         ZeroMemory VARPTR(DeviceBroadCastDeviceInterface), SIZEOF(DeviceBroadCastDeviceInterface)
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICEARRIVAL"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVICEQUERYREMOVE                                           'System uses WM_DEVICECHANGE, wParam = DBT_DEVICEQUERYREMOVE, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICEQUERYREMOVE"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVICEQUERYREMOVEFAILED                                     'System uses WM_DEVICECHANGE, wParam = DBT_DEVICEQUERYREMOVEFAILED, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICEQUERYREMOVEFAILED"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVICEREMOVECOMPLETE                                        'System uses WM_DEVICECHANGE, wParam = DBT_DEVICEREMOVECOMPLETE, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICEREMOVECOMPLETE"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                             SELECT CASE DevNodesChanged
                                  CASE %FALSE                                                 'Flag not set
                                  CASE %TRUE                                                  'Flag set so unset it
                                       DevNodesChanged = %FALSE
                             END SELECT
                        CASE %DBT_DEVICEREMOVEPENDING                                         'System uses WM_DEVICECHANGE, wParam = DBT_DEVICEREMOVEPENDING, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICEREMOVEPENDING"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVICETYPESPECIFIC                                          'System uses WM_DEVICECHANGE, wParam = DBT_DEVICETYPESPECIFIC, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVICETYPESPECIFIC"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE %DBT_DEVNODES_CHANGED                                            'System uses WM_DEVICECHANGE, wParam = DBT_DEVNODES_CHANGED, lParam = 0
                             Message = Message + SPACE$(5) + "wParam = DBT_DEVNODES_CHANGED" + SPACE$(5) + STR$(lParam)
                             SELECT CASE DevNodesChanged                                      'Stop 'Idle' %DBT_DEVNODES_CHANGED messages
                                  CASE %FALSE                                                 '1st time flagged
                                       DevNodesChanged = %TRUE
                                  CASE %TRUE                                                  'Already showed so ignore till flag is reset
                                       Message = ""
                             END SELECT
    'Message = Message + space$(5) + Str(glngPrevDeviceNotifyProc) + vbCr + Str(hWnd)
                        CASE %DBT_QUERYCHANGECONFIG                                           'System uses WM_DEVICECHANGE, wParam = DBT_QUERYCHANGECONFIG, lParam = 0
                             Message = Message + SPACE$(5) + "wParam = DBT_QUERYCHANGECONFIG"
                        CASE %DBT_USERDEFINED                                                 'System uses WM_DEVICECHANGE, wParam = DBT_USERDEFINED, lParam = TBD
                             Message = Message + SPACE$(5) + "wParam = DBT_USERDEFINED"
                             Message=Message + GetUdtDeviceEvent(DeviceBroadCastHeader, DeviceBroadCastOem, DeviceBroadCasteDevNode, DeviceBroadCastVolume, DeviceBroadCastPort, DeviceBroadCastNet, DeviceBroadCastDeviceInterface, DeviceBroadCastHandle, lParam)
                        CASE ELSE
                             Message = Message + SPACE$(5) + "wParam = " & STR$(wParam) & $TAB & "Device unknown: " & STR$(DeviceBroadCastHeader.DbhDevicetype)
                   END SELECT
                   SELECT CASE Message
                        CASE ""
                        CASE ELSE
                             AddToList Message
                   END SELECT
                   Message = ""
         END SELECT
         DeviceNotifyProc = CallWindowProc(glngPrevDeviceNotifyProc, hWnd, Msg, wParam, lParam)       ' pass the rest messages to original function
    END FUNCTION
    
    FUNCTION DoRegisterDeviceInterface(hWnd AS LONG, BYREF hDevNotify AS LONG) AS LONG
         LOCAL Message AS STRING
         LOCAL NotificationFilter AS DEV_BROADCAST_DEVICEINTERFACE
         NotificationFilter.DbDiSize = SIZEOF(NotificationFilter)
         NotificationFilter.DbDiDevicetype = %DBT_DEVTYP_DEVICEINTERFACE
         hDevNotify = RegisterDeviceNotification(hWnd, VARPTR(NotificationFilter), %DEVICE_NOTIFY_WINDOW_HANDLE OR %DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)
         IF hDevNotify = 0 THEN
              Message = Message + SPACE$(5) + "RegisterDeviceNotification failed: "
              FUNCTION = %False
              EXIT FUNCTION
         END IF
        FUNCTION = %True
    END FUNCTION
    
    FUNCTION GetUdtDeviceEvent( _
                                  DeviceBroadCastHeader AS DEV_BROADCAST_HDR, _
                                  DeviceBroadCastOem AS DEV_BROADCAST_OEM, _
                                  DeviceBroadCasteDevNode AS DEV_BROADCAST_DEVNODE, _
                                  DeviceBroadCastVolume AS DEV_BROADCAST_VOLUME, _
                                  DeviceBroadCastPort AS DEV_BROADCAST_PORT, _
                                  DeviceBroadCastNet AS DEV_BROADCAST_NET, _
                                  DeviceBroadCastDeviceInterface AS DEV_BROADCAST_DEVICEINTERFACE, _
                                  DeviceBroadCastHandle AS DEV_BROADCAST_HANDLE, _
                                  BYVAL lParam AS LONG _
                             )AS STRING
         LOCAL Message AS STRING
         LOCAL TempBin AS STRING
         LOCAL i AS LONG
         LOCAL DriveName AS STRING
         LOCAL DriveFlags AS STRING
         STATIC DevNodesChanged AS LONG
                             CopyMemory VARPTR(DeviceBroadCastHeader), BYVAL (lParam), SIZEOF(DeviceBroadCastHeader)
                             SELECT CASE DeviceBroadCastHeader.DbhDevicetype                  'Determine Device Type
                                  CASE %DBT_DEVTYP_OEM                                        'Original Equipment Manufacturer(OEM) or Independent Hardware Vendor(IHV) defined device type. This structure is a DEV_BROADCAST_OEM structure.
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_OEM"
                                       CopyMemory VARPTR(DeviceBroadCastOem), BYVAL (lParam), SIZEOF(DeviceBroadCastOem)
                                       Message = Message + SPACE$(5) + "Device Identifier: " & STR$(DeviceBroadCastOem.DboIdentifier) & $TAB & "Device Function: " & STR$(DeviceBroadCastOem.DboSuppFunc)
                                  CASE %DBT_DEVTYP_DEVNODE                                    'Devnode number (specific to Windows 95) '<--- No structure found to pass
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_DEVNODE"
                                       CopyMemory VARPTR(DeviceBroadCasteDevNode), BYVAL (lParam), SIZEOF(DeviceBroadCasteDevNode)
                                       Message = Message + SPACE$(5) + "Device Node: " & STR$(DeviceBroadCasteDevNode.DbnDevNode)
                                  CASE %DBT_DEVTYP_VOLUME                                     'Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_VOLUME"
                                       CopyMemory VARPTR(DeviceBroadCastVolume), BYVAL (lParam), SIZEOF(DeviceBroadCastVolume)
                                       TempBin = BIN$(DeviceBroadCastVolume.DbvUnitMask)
                                       TempBin = STRREVERSE$(TempBin)
                                       FOR i = 1 TO LEN(TempBin)
                                            SELECT CASE VAL(MID$(TempBin, i, 1))
                                                 CASE 0                                       'Bit not set
                                                 CASE 1                                       'Bit set
                                                      DriveName = CHR$(64 + i) + ":\"         'Chr(64) = @, chr$(65) = A, Chr(66)= B, etc...
                                                      EXIT FOR
                                            END SELECT
                                       NEXT i
                                       SELECT CASE DeviceBroadCastVolume.dbvFlags
                                            CASE 0                                            'Local Volume
                                                 DriveFlags = "Local Drive"
                                            CASE %DBTF_MEDIA                                  'Media Volume
                                                 DriveFlags = "Media Drive"
                                            CASE %DBTF_NET                                    'Network volume
                                                 DriveFlags = "Network Drive"
                                       END SELECT
                                       Message = Message + SPACE$(5) + "Device Name: " & DriveName + SPACE$(5) + "Drive Info:" + DriveFlags
                                  CASE %DBT_DEVTYP_PORT                                       'Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT structure.
    '#debug print str$(wParam) + space$(5) + str$(%DBT_DEVICEARRIVAL)
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_PORT"
                                       CopyMemory VARPTR(DeviceBroadCastPort), BYVAL (lParam), SIZEOF(DeviceBroadCastPort)
                                       Message = Message + SPACE$(5) + "Device Name: " & DeviceBroadCastPort.DbpName
                                  CASE %DBT_DEVTYP_NET                                        'Network Resource (UNC)
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_NET"
                                       CopyMemory VARPTR(DeviceBroadCastNet), BYVAL (lParam), SIZEOF(DeviceBroadCastNet)
                                       Message = Message + SPACE$(5) + "Device Name: " & DeviceBroadCastNet.DbnName
                                  CASE %DBT_DEVTYP_DEVICEINTERFACE                            'Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_DEVICEINTERFACE"
                                       CopyMemory VARPTR(DeviceBroadCastDeviceInterface), BYVAL (lParam), BYVAL (DeviceBroadCastHeader.DbhSize)
                                       Message = Message + SPACE$(5) + "Device Name: " & DeviceBroadCastDeviceInterface.DbDiName & $TAB & "GUID: " & GUIDTXT$(DeviceBroadCastDeviceInterface.DbDiClassguid)
                                  CASE %DBT_DEVTYP_HANDLE                                     'File system handle. This structure is a DEV_BROADCAST_HANDLE structure.
                                       Message = Message + SPACE$(5) + "Device Type: DBT_DEVTYP_HANDLE"
                                       CopyMemory VARPTR(DeviceBroadCastHandle), BYVAL (lParam), SIZEOF(DeviceBroadCastHandle)
                                       Message = Message + SPACE$(5) + "Device Handle: " & STR$(DeviceBroadCastHandle.DbhHandle) + SPACE$(5) + "Device Handle Notify: " + STR$(DeviceBroadCastHandle.DbhHdevNotify)
                                       Message = Message + SPACE$(5) + "Device Guid: " & GUIDTXT$(DeviceBroadCastHandle.DbhEventGuid) + SPACE$(5) + "Event Offset: " + STR$(DeviceBroadCastHandle.DbhNameOffset)
                                       Message = Message + SPACE$(5) + "Binary Data: " & STR$(DeviceBroadCastHandle.DbhData(0))
                                  CASE ELSE
                                       Message = Message + SPACE$(5) + "Device Type unknown: " & STR$(DeviceBroadCastHeader.DbhDevicetype)
                                       CopyMemory VARPTR(DeviceBroadCastDeviceInterface), BYVAL (lParam), BYVAL (DeviceBroadCastHeader.DbhSize)
                                       Message = Message + SPACE$(5) + "Device Name: " & STR$(LEN(DeviceBroadCastDeviceInterface.DbDiName)) & MID$(DeviceBroadCastDeviceInterface.DbDiName, 1, INSTR(DeviceBroadCastDeviceInterface.DbDiName, CHR$(0)))
                             END SELECT
         FUNCTION = Message
    END FUNCTION
    #ENDIF
    DemoDeviceNotify.bas
    Code:
    #COMPILE EXE
    #DIM ALL
    GLOBAL hDlg  AS DWORD
    
    '------------------------------------------------------------------------------
    '   ** Includes **
    '------------------------------------------------------------------------------
    #IF NOT %DEF(%WINAPI)
        #INCLUDE "WIN32API.INC"
    #ENDIF
    #IF NOT %DEF(%DEVICENOTIFY)
         #INCLUDE "DeviceNotify.INC"
    #ENDIF
    '------------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------
    '   ** Constants **
    '------------------------------------------------------------------------------
    %IDD_DIALOG1 = 101
    %TxtDeviceNotify = 102
    '------------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------
    '   ** Declarations **
    '------------------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
    DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    '------------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------
    '   ** Main Application Entry Point **
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN()
        ShowDIALOG1 %HWND_DESKTOP
    END FUNCTION
    '------------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------
    '   ** CallBacks **
    '------------------------------------------------------------------------------
    CALLBACK FUNCTION ShowDIALOG1Proc()
        SELECT CASE AS LONG CBMSG
            CASE %WM_INITDIALOG
                ' Initialization handler
            CASE %WM_NCACTIVATE
                STATIC hWndSaveFocus AS DWORD
                IF ISFALSE CBWPARAM THEN
                    ' Save control focus
                    hWndSaveFocus = GetFocus()
                ELSEIF hWndSaveFocus THEN
                    ' Restore control focus
                    SetFocus(hWndSaveFocus)
                    hWndSaveFocus = 0
                END IF
            CASE %WM_COMMAND
                ' Process control notifications
                SELECT CASE AS LONG CBCTL
                     CASE %TxtDeviceNotify
                END SELECT
        END SELECT
    END FUNCTION
    '------------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------
    '   ** Dialogs **
    '------------------------------------------------------------------------------
    FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
         LOCAL lRslt AS LONG
         LOCAL Success AS LONG
        DIALOG NEW hParent, "Dialog1", 70, 70, 750, 230, %WS_POPUP OR %WS_BORDER _
            OR %WS_DLGFRAME OR %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
            %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR _
            %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
            %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
            %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
    
         CONTROL ADD TEXTBOX, hDlg, %TxtDeviceNotify, "Unplug and re-seat USB connection to see results", 5, 5, 740, 225, _ 'Create Textbox
              %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %WS_HSCROLL OR _
              %WS_VSCROLL OR %ES_LEFT OR %ES_MULTILINE OR %ES_AUTOHSCROLL OR _
              %ES_AUTOVSCROLL, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR _
              %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
    
         CALL DoRegisterDeviceInterface(hDlg , hDevNotify)                '<--- DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE are automatically broadcast for port devices.
                                                                          '<--- so not necessary to RegisterDeviceNotification for DBT_DEVTYP_PORT OR DBT_DEVTYP_VOLUME OR DBT_DEVTYP_OEM
         glngPrevDeviceNotifyProc = GetWindowLong(hDlg , %GWL_WNDPROC)
         SetWindowLong hDlg , %GWL_WNDPROC, CODEPTR(DeviceNotifyProc)
         DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
         IF hDevNotify <> 0 THEN                                          '<--- If RegisterDeviceInterface was used, then release it (or the next compile will not find the device)
              Success = UnregisterDeviceNotification(hDevNotify)
         END IF
         FUNCTION = lRslt
    END FUNCTION
    '------------------------------------------------------------------------------
    
    SUB AddToList (BYVAL LbString AS STRING)
         LOCAL TempText AS STRING
         STATIC HoldText AS STRING
         CONTROL GET TEXT hDlg, %TxtDeviceNotify TO TempText                   'Get the formatted text
         SELECT CASE HoldText
              CASE TempText
              CASE <> TempText
                   HoldText = TempText
         END SELECT
         CONTROL SET TEXT hDlg, %TxtDeviceNotify, HoldText  + $CRLF + LbString         'Show the formatted text
    END SUB
    Discussion, Hints, Help can be found at http://www.powerbasic.com/support/pb...528#post307528

    And as Requested a zipped version of the program recompiled in PB 8.04
    Attached Files
    Last edited by Cliff Nichols; 23 Jan 2009, 02:56 PM. Reason: Add Zip
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Okay, this copy works!
    Thank you.

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

    Comment


    • #3
      It is always a pleasure to ask in the forum.

      Comment


      • #4
        Cliff,
        Works like a charm on PB10.04 WIN7 Home Premium! It's gonna take me a while to digest all the info.
        It's impossible to make anything foolproof because fools are so ingenious.

        Comment

        Working...
        X