News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Detecting USb device

Started by starzboy, July 22, 2009, 08:05:43 PM

Previous topic - Next topic

starzboy

Hello
I have been experimenting again lately.
I can usually just check getDrivetype to verify a USB device and identify it, but I was wondering what would be the best way to detect it as soon as a USB device is plugged into the system.
There is one way to use a timer and getdrivetype of all logical drives, but I was wondering if is possible to get info as soon as a device is plugged in.

Any help will highly be appreciated.
Thankyou

redskull

I would assume you would be sent a WM_DEVICECHANGE message, but don't quote me on it...

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

fearless

ƒearless

MichaelW

Intercepting WM_DEVICECHANGE appears to work, but in my limited test plugging/unplugging my USB mouse I get only DBT_DEVNODES_CHANGED events. For a USB Flash drive I get DBT_DEVNODES_CHANGED, DBT_DEVICEARRIVAL, and DBT_DEVICEREMOVECOMPLETE events.

dbt.inc:

;================================================================
; These value names are from the list of possible events for the
; WM_DEVICECHANGE message, and the values are from Dbt.h, both
; from the Windows Server 2003 PSDK.
;================================================================

DBT_DEVNODES_CHANGED equ 0007h

DBT_QUERYCHANGECONFIG equ 0017h

DBT_CONFIGCHANGED equ 0018h

DBT_CONFIGCHANGECANCELED equ 0019h

DBT_DEVICEARRIVAL equ 8000h

DBT_DEVICEQUERYREMOVE equ 8001h

DBT_DEVICEQUERYREMOVEFAILED equ 8002h

DBT_DEVICEREMOVEPENDING equ 8003h

DBT_DEVICEREMOVECOMPLETE equ 8004h

DBT_DEVICETYPESPECIFIC equ 8005h

DBT_CUSTOMEVENT equ 8006h

DBT_USERDEFINED equ 0ffffh


; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
    include dbt.inc

    IDC_LIST equ 1000

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      hInstance   dd 0
      hwndList    dd 0
      buff        db 100 dup(0)
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

DlgProc proc hwndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

    LOCAL rc:RECT

    SWITCH uMsg

      CASE WM_INITDIALOG

        invoke GetDlgItem, hwndDlg, IDC_LIST
        mov hwndList, eax

        invoke GetClientRect, hwndDlg, ADDR rc
        invoke MoveWindow, hwndList, 0, 0, rc.right, rc.bottom, TRUE

      CASE WM_DEVICECHANGE

        SWITCH wParam

          CASE DBT_DEVNODES_CHANGED

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVNODES_CHANGED")

          CASE DBT_QUERYCHANGECONFIG

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_QUERYCHANGECONFIG")

          CASE DBT_CONFIGCHANGED

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_CONFIGCHANGED")

          CASE DBT_CONFIGCHANGECANCELED

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_CONFIGCHANGECANCELED")

          CASE DBT_DEVICEARRIVAL

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICEARRIVAL")

          CASE DBT_DEVICEQUERYREMOVE

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICEQUERYREMOVE")

          CASE DBT_DEVICEQUERYREMOVEFAILED

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICEQUERYREMOVEFAILED")

          CASE DBT_DEVICEREMOVEPENDING

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICEREMOVEPENDING")

          CASE DBT_DEVICEREMOVECOMPLETE

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICEREMOVECOMPLETE")

          CASE DBT_DEVICETYPESPECIFIC

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_DEVICETYPESPECIFIC")

          CASE DBT_CUSTOMEVENT

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_CUSTOMEVENT")

          CASE DBT_USERDEFINED

            invoke SendMessage, hwndList, LB_ADDSTRING, 0,
                                chr$("DBT_USERDEFINED")

          DEFAULT

            invoke SendMessage, hwndList, LB_ADDSTRING, 0, uhex$(wParam)

        ENDSW

        ;-----------------------------------
        ; Scroll last item added into view.
        ;-----------------------------------

        invoke SendMessage, hwndList, LB_GETCOUNT, 0, 0
        sub eax, 1
        invoke SendMessage, hwndList, LB_SETTOPINDEX, eax, 0

      CASE WM_COMMAND

        SWITCH wParam

          CASE IDCANCEL

            invoke EndDialog, hwndDlg, 0

        ENDSW

      CASE WM_CLOSE

        invoke EndDialog, hwndDlg, 0

    ENDSW

    return 0

DlgProc endp

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    invoke GetModuleHandle, NULL
    mov hInstance, eax

    Dialog "Test","MS Sans Serif",10, \
            WS_OVERLAPPED or WS_SYSMENU or DS_CENTER, \
            1,0,0,120,90,1024

    DlgList WS_VSCROLL or LBS_NOINTEGRALHEIGHT,0,0,0,0,IDC_LIST

    CallModalDialog hInstance,0,DlgProc,NULL

    exit

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
eschew obfuscation