flat assembler
Message board for the users of flat assembler.

Index > Windows > USB RegisterDeviceNotificationA

Author
Thread Post new topic Reply to topic
swank



Joined: 07 Nov 2018
Posts: 6
swank 05 Jan 2024, 01:43
Hello guys,

I'm trying to call the API RegisterDeviceNotificationA in order to receive notifications regarding PnP new devices. But I need to identify all messages, not just the default ones.

According to the documentation remarks [1], "The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events are automatically broadcast to all top-level windows for port devices. Therefore, it is not necessary to call RegisterDeviceNotification for ports, and the function fails if the dbch_devicetype member is DBT_DEVTYP_PORT. Volume notifications are also broadcast to top-level windows, so the function fails if dbch_devicetype is DBT_DEVTYP_VOLUME. OEM-defined devices are not used directly by the system, so the function fails if dbch_devicetype is DBT_DEVTYP_OEM."

That said, I updated the ManHunter/PCL USB Plug'n'Play Demo [2] (thanks mate), in order to receive the not standard PnP notifications.

Code:
;---------------------------------------------
; USB Plug'n'Play Demo
; Copyright (C) ManHunter / PCL
; http://www.manhunter.ru
;---------------------------------------------

format PE GUI 4.0
entry start

include 'win32a.inc'

ID_TXT = 101

;---------------------------------------------

section '.data' data readable writeable

szOn                    db 'USB drive '
drv1                    db 'X: connected',13,10,0

szOff                   db 'USB drive '
drv2                    db 'X: removed',13,10,0
hwnd                    dd ?

hbuff                   dd ?
hhndl                   dd ?

DBT_DEVICEARRIVAL            = 0x8000
DBT_DEVICEREMOVECOMPLETE     = 0x8004

DBT_DEVTYP_VOLUME            = 0x00000002
DBT_DEVTYP_DEVICEINTERFACE   = 0x00000005
DBT_DEVTYP_OEM               = 0x00000000
DBT_DEVTYP_PORT              = 0x00000003
DBT_DEVTYP_HANDLE            = 0x00000006

DEVICE_NOTIFY_WINDOW_HANDLE  = 0x00000000
DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 0x00000004

struct DEV_BROADCAST
        ; Structure header
        dbch_size       dd ?
        dbch_devicetype dd ?
        dbch_reserved   dd ?

        ; Information part of the structure
        dbcv_unitmask   dd ?
        dbcv_flags      dd ?
ends

; Âèðòóàëüíàÿ ïðîåêöèÿ ñòðóêòóðû DEV_BROADCAST
virtual at 0
event   DEV_BROADCAST
end     virtual

;---------------------------------------------

section '.code' code readable executable

  start:
        invoke  GetModuleHandle,0
        mov     [hwnd], eax

        invoke GetProcessHeap
        mov [hhndl],eax

        invoke HeapAlloc,[hhndl],HEAP_NO_SERIALIZE,100h
        mov [hbuff],eax

        invoke  RegisterDeviceNotification, [hwnd], [hbuff], DEVICE_NOTIFY_ALL_INTERFACE_CLASSES
        invoke  DialogBoxParam,[hwnd],37,HWND_DESKTOP,DialogProc,0
        invoke  ExitProcess,0

;---------------------------------------------

proc DialogProc hwnddlg,msg,wparam,lparam
        push    ebx esi edi
        cmp     [msg],WM_INITDIALOG
        je      wminitdialog
        cmp     [msg],WM_COMMAND
        je      wmcommand
        cmp     [msg],WM_CLOSE
        je      wmclose
        ; Ïðèøëî ñîîáùåíèå îá èçìåíåíèè ñîñòîÿíèÿ ñúåìíîãî íàêîïèòåëÿ
        cmp     [msg],WM_DEVICECHANGE
        je      update_usb
        xor     eax,eax
        jmp     finish

update_usb:
        ; Óñòðîéñòâî ïîäêëþ÷åíî?
        cmp     [wparam],DBT_DEVICEARRIVAL
        je      usb_connected

        ; Óñòðîéñòâî èçâëå÷åíî?
        cmp     [wparam],DBT_DEVICEREMOVECOMPLETE
        je      usb_disconnected

        jmp     processed

        ; Îáðàáîòêà ïîäêëþ÷åíèÿ óñòðîéñòâà
usb_connected:
        ; Óêàçàòåëü íà ñòðóêòóðó DEV_BROADCAST
        mov     esi,[lparam]
        ; Ñîîáùåíèå ïðèøëî îò ëîãè÷åñêîãî äèñêà?
        cmp     dword [esi+event.dbch_devicetype],DBT_DEVTYP_VOLUME
        jne     processed

        ; Ìàñêà äèñêîâ
        mov     eax,[esi+event.dbcv_unitmask]

        ; Ïîëó÷èòü áóêâó äèñêà èç ìàñêè
        bsr     eax,eax
        add     al,'A'

        ; AL - áóêâà äèñêà
        mov     byte [drv1],al

        ; Çàïèñàòü ñòðî÷êó â ëîã
        stdcall AddLog,[hwnddlg],ID_TXT,szOn
        jmp     processed

        ; Îáðàáîòêà îòêëþ÷åíèÿ óñòðîéñòâà
usb_disconnected:
        ; Óêàçàòåëü íà ñòðóêòóðó DEV_BROADCAST
        mov     esi,[lparam]
        ; Ñîîáùåíèå ïðèøëî îò ëîãè÷åñêîãî äèñêà?
        cmp     dword [esi+event.dbch_devicetype],DBT_DEVTYP_VOLUME
        jne     processed

        ; Ìàñêà äèñêîâ
        mov     eax,[esi+event.dbcv_unitmask]

        ; Áóêâà äèñêà ïðèñóòñòâóåò?
        or      eax,eax
        jz      processed

        ; Ïîëó÷èòü áóêâó äèñêà èç ìàñêè
        bsr     eax,eax
        add     al,'A'

        ; AL - áóêâà äèñêà
        mov     byte [drv2],al

        ; Çàïèñàòü ñòðî÷êó â ëîã
        stdcall AddLog,[hwnddlg],ID_TXT,szOff
        jmp     processed

wminitdialog:
        jmp     processed
wmcommand:
        cmp     [wparam],BN_CLICKED shl 16 + IDCANCEL
        je      wmclose
        jmp     processed

wmclose:
        invoke  EndDialog,[hwnddlg],0
processed:
        mov     eax,1
finish:
        pop     edi esi ebx
        ret
endp

;---------------------------------------------

proc    AddLog  hWnd:dword,CtrlID:dword,pStr:dword
        push    eax
        invoke  GetDlgItem,[hWnd],[CtrlID]
        or      eax,eax
        jz      .AddLog_1
        mov     [CtrlID],eax
        invoke  SendMessage,[CtrlID],EM_GETLINECOUNT,0,0
        dec     eax
        invoke  SendMessage,[CtrlID],EM_LINEINDEX,eax,0
        invoke  SendMessage,[CtrlID],EM_SETSEL,eax,eax
        invoke  SendMessage,[CtrlID],EM_REPLACESEL,FALSE,[pStr]
.AddLog_1:
        pop     eax
        ret
endp

;---------------------------------------------

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL'

  include 'api\kernel32.inc'
  include 'api\user32.inc'

;---------------------------------------------

section '.rsrc' resource data readable

  directory RT_DIALOG,dialogs

  resource dialogs,\
           37,LANG_ENGLISH+SUBLANG_DEFAULT,demonstration

  dialog demonstration,"Plug'n'Play Demo",0,0,180,160,WS_CAPTION+DS_CENTER+WS_POPUP+WS_SYSMENU+DS_MODALFRAME+DS_SYSMODAL
    dialogitem 'EDIT','',ID_TXT,5,5,170,130,WS_VISIBLE+WS_BORDER+ES_READONLY+ES_MULTILINE+WS_VSCROLL+WS_HSCROLL
    dialogitem 'BUTTON','Exit',IDCANCEL,130,140,45,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON
  enddialog
    


But the API RegisterDeviceNotificationA return NULL with the LastError (ERROR_INVALID_DATA).

Could you guys give me some light? Thanks!

1. https://learn.microsoft.com/en-us/windows/win32/api/dbt/ns-dbt-dev_broadcast_hdr

2. https://www.manhunter.ru/assembler/402_obrabotka_podklyucheniya_i_otklyucheniya_semnogo_nakopitelya.html
Post 05 Jan 2024, 01:43
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1040
Location: Russia
macomics 05 Jan 2024, 09:29
The GetModuleHandle function returns you base address of loading the corresponding module into memory (HINSTANCE), not the window handle (HWND). For the RegisterDeviceNotificationA function, first parameter should be exactly the window handle. To receive messages in the dialog box, you need to call this function in WM_INITDIALOG and pass it the dialog box identifier (hwnddlg).
Post 05 Jan 2024, 09:29
View user's profile Send private message Reply with quote
swank



Joined: 07 Nov 2018
Posts: 6
swank 05 Jan 2024, 17:18
Thanks! Helped a lot!
Post 05 Jan 2024, 17:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.