News:

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

Activated only with drive G:

Started by Magnum, March 26, 2011, 05:57:17 PM

Previous topic - Next topic

Magnum

This works fine when my thumb drive is plugged in, but unfortunately it
is also activated when I turn on my printer or insert a CD or DVD.

I need it to only work for drive G:.


include \masm32\include\masm32rt.inc

CTEXT macro Text
   local szText
   .data
   szText byte Text, 0
   .code
   exitm <offset szText>  
endm


DBT_DEVICEARRIVAL        equ 8000h
DBT_DEVICEREMOVECOMPLETE equ 8004h


.data

No_Batch_File  db      "Batch file is not present.",0
Batch_File     db      "C:\Bat\BAK_ALL.bat",0
AppName        db      "UsbToHD",0

szClassName BYTE "USB_Watcher", 0

.data?

StartupInfo        STARTUPINFO         <?>
szComspec        db MAX_PATH dup(?)
szAppname        db MAX_PATH dup(?)
ProcessInfo        PROCESS_INFORMATION <?>

hInst DWORD ?
hMain DWORD ?
wc WNDCLASSEX <?>
msg MSG <?>

.code

USBWatcher:

invoke GetModuleHandle, NULL
mov hInst, eax

mov wc.hInstance, eax
mov wc.lpszClassName, offset szClassName
mov wc.lpfnWndProc, offset WndProc
mov wc.style, CS_DBLCLKS
mov wc.cbSize, sizeof WNDCLASSEX
mov wc.hIcon, NULL
mov wc.hIconSm, NULL
mov wc.hCursor, NULL
mov wc.lpszMenuName, NULL
mov wc.cbClsExtra, 0
mov wc.cbWndExtra, 0
mov wc.hbrBackground, NULL
invoke RegisterClassEx, addr wc

invoke CreateWindowEx,
NULL, \ ; don't need this
offset szClassName, \
NULL, \
NULL, \ ; don't need a window style
0, 0, 0, 0, \ ; don't need top, left, width or height
NULL, \ ; message only window
NULL, \
hInst, \
NULL
mov hMain, eax

.while TRUE
invoke GetMessage,addr msg,NULL,0,0
 .break .if !eax
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
.endw
invoke ExitProcess, 0

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

  .if uMsg==WM_DEVICECHANGE
  .if wParam == DBT_DEVICEARRIVAL
;invoke MessageBox, NULL, offset szMsgConnected, offset szClassName, MB_OK or MB_ICONINFORMATION

  ; Check if batch file is present
invoke GetFileAttributes,offset Batch_File

.IF EAX == 0FFFFFFFFh
  invoke MessageBox, 0, ADDR No_Batch_File, ADDR AppName,MB_ICONINFORMATION
  invoke  ExitProcess,NULL
.ENDIF

     invoke GetStartupInfo, addr StartupInfo
invoke GetEnvironmentVariable, CTEXT('ComSpec'), addr szComspec, sizeof szComspec
invoke wsprintf, addr szAppname, CTEXT('"%s" /c "C:\Bat\BAK_ALL.bat"'), addr szComspec
invoke CreateProcess, 0, addr szAppname, 0, 0, FALSE, \
NORMAL_PRIORITY_CLASS, 0, 0, addr StartupInfo, addr ProcessInfo

     invoke WaitForSingleObject, ProcessInfo.hProcess, INFINITE

           .elseif wParam == DBT_DEVICEREMOVECOMPLETE
;invoke MessageBox, hWnd, offset szMsgDisCon, offset szClassName, MB_OK or MB_ICONINFORMATION
;invoke SendMessage, hWnd, WM_CLOSE, NULL, NULL

.endif
.elseif uMsg==WM_CLOSE
;invoke MessageBox, NULL, offset szMsgClose, offset szClassName, MB_OK or MB_ICONINFORMATION
;invoke PostQuitMessage, 0
ret
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret

.endif
xor eax, eax
ret
WndProc endp


Have a great day,
                         Andy

oex

Check for drive G

GetLogicalDrives
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

baltoro

I remember running into a user-mode sample application (the source code was from the DDK) that did something similar. It was called: USB View.
From the documentation: Usbview.exe is a Windows GUI application that allows you to browse all USB controllers and connected USB devices on your system.
The source code is in C. In that example, all the device controllers were enumerated, and information retrieved using repeated calls to: DeviceIoControl.
It's obviously alot more intensive than what you need here; and, USB View does not enumerate devices on pre-Windows XP SP1-based computers.
Baltoro

qWord

look for lParam: it is an pointer to an struct containgin information about the device -> DBT_DEVICEARRIVAL Event
FPU in a trice: SmplMath
It's that simple!

oex

Quote from: qWord on March 26, 2011, 06:34:55 PM
look for lParam: it is an pointer to an struct containgin information about the device -> DBT_DEVICEARRIVAL Event

Out of interest did this not work?
http://www.masm32.com/board/index.php?topic=13899.msg109904#msg109904

I remember I had some problem recognising my MP3 player.... It has storage but did not register as a volume or somesuch....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Magnum

This will work if I can figure out how to test if bit 6 has been set.

I looked at the info for test and some old code, but could not find what I was looking for.


CheckDriveC proc    uses ebx esi edi ebp    DriveList: DWORD
            mov     eax, DriveList
            and     eax, 4          ; if bit #2 is set -> C is available
            shr     eax,2               ; bit 6 is G:
Have a great day,
                         Andy

oex

Quote from: Magnum on March 26, 2011, 07:20:59 PM
This will work if I can figure out how to test if bit 6 has been set.

I looked at the info for test and some old code, but could not find what I was looking for.


CheckDriveC proc    uses ebx esi edi ebp    DriveList: DWORD
            mov     eax, DriveList
            and     eax, 4          ; if bit #2 is set -> C is available
            shr     eax,2               ; bit 6 is G:


bt instruction
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Magnum

Getting "Cannot nest procedures."

Code works, maybe I messed up some .if statement.


; Usb_Watcher.asm
;
include \masm32\include\masm32rt.inc

CheckDriveC proto :DWORD

CTEXT macro Text
    local szText
    .data
    szText byte Text, 0
    .code
    exitm <offset szText>   
endm


DBT_DEVICEARRIVAL        equ 8000h
DBT_DEVICEREMOVECOMPLETE equ 8004h


.data

No_Batch_File  db      "Batch file is not present.",0
Batch_File     db      "C:\Bat\BAK_ALL.bat",0
AppName        db      "UsbToHD",0

szClassName BYTE "USB_Watcher", 0

.data?

StartupInfo         STARTUPINFO         <?>
szComspec         db MAX_PATH dup(?)
szAppname         db MAX_PATH dup(?)
ProcessInfo         PROCESS_INFORMATION <?>

hInst DWORD ?
hMain DWORD ?
wc WNDCLASSEX <?>
msg MSG <?>

.code

USBWatcher:

invoke GetModuleHandle, NULL
mov hInst, eax

mov wc.hInstance, eax
mov wc.lpszClassName, offset szClassName
mov wc.lpfnWndProc, offset WndProc
mov wc.style, CS_DBLCLKS
mov wc.cbSize, sizeof WNDCLASSEX
mov wc.hIcon, NULL
mov wc.hIconSm, NULL
mov wc.hCursor, NULL
mov wc.lpszMenuName, NULL
mov wc.cbClsExtra, 0
mov wc.cbWndExtra, 0
mov wc.hbrBackground, NULL
invoke RegisterClassEx, addr wc

invoke CreateWindowEx,
NULL, \ ; don't need this
offset szClassName, \
NULL, \
NULL, \ ; don't need a window style
0, 0, 0, 0, \ ; don't need top, left, width or height
NULL, \ ; message only window
NULL, \
hInst, \
NULL
mov hMain, eax

.while TRUE
invoke GetMessage,addr msg,NULL,0,0
  .break .if !eax
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
.endw
invoke ExitProcess, 0

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

  .if uMsg==WM_DEVICECHANGE
  .if wParam == DBT_DEVICEARRIVAL

   ; Check if batch file is present
invoke GetFileAttributes,offset Batch_File

.IF EAX == 0FFFFFFFFh
   invoke MessageBox, 0, ADDR No_Batch_File, ADDR AppName,MB_ICONINFORMATION
   invoke  ExitProcess,NULL
.ENDIF

invoke  GetLogicalDrives        ; get drives
invoke  CheckDriveC, eax        ; check if drive G: is available

CheckDriveC proc    uses ebx esi edi ebp    DriveList: DWORD

            mov   eax, DriveList
            bt eax, 6 ; check if bit 6 is set (Drive G:)
            ret
           
CheckDriveC endp

        .if     (eax == 7Ch)
               
                jmp continue
        .else

        invoke  ExitProcess, 0
       
        .endif

continue:

      invoke GetStartupInfo, addr StartupInfo
invoke GetEnvironmentVariable, CTEXT('ComSpec'), addr szComspec, sizeof szComspec
invoke wsprintf, addr szAppname, CTEXT('"%s" /c "C:\Bat\BAK_ALL.bat"'), addr szComspec
invoke CreateProcess, 0, addr szAppname, 0, 0, FALSE, \
NORMAL_PRIORITY_CLASS, 0, 0, addr StartupInfo, addr ProcessInfo

      invoke WaitForSingleObject, ProcessInfo.hProcess, INFINITE

            .elseif wParam == DBT_DEVICEREMOVECOMPLETE


.endif
.elseif uMsg==WM_CLOSE

ret
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret

.endif
xor eax, eax
ret
WndProc endp

end USBWatcher
Have a great day,
                         Andy

oex

You need to put:

CheckDriveC proc    uses ebx esi edi ebp    DriveList: DWORD

            mov   eax, DriveList
            bt   eax, 6 ; check if bit 6 is set (Drive G:)
            ret
           
CheckDriveC endp

outside the WndProc PROC
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Magnum

I have to check more drives because drive letter depends on whether printer is turned on.

I got C:\masm32\SOURCE\Usb_Watcher.asm(111) : error A2170: directive must appear inside a macro


invoke  GetLogicalDrives        ; get drives
invoke  CheckDriveG, eax        ; check if drive G: is available

        push eax ; save for later use

        .if     (eax == 7Ch)

           ; check if G:drive has a Backup directory
           ; G: may be the printer drive
           
           invoke GetFileAttributes,offset G_USB_Directory

        .IF (EAX == 0FFFFFFFFh)

          goto check_4_H_Drive 

        .ENDIF

        .endif

check_4_H_Drive: ; bit 7 is H: drive

         pop eax

         .if (eax == 0F8h)   

         invoke GetFileAttributes,offset H_USB_Directory

        .IF (EAX == 0FFFFFFFFh)

        invoke  ExitProcess, 0
       
        .endif
        .endif

continue:

      invoke GetStartupInfo, addr StartupInfo
invoke GetEnvironmentVariable, CTEXT('ComSpec'), addr szComspec, sizeof szComspec
invoke wsprintf, addr szAppname, CTEXT('"%s" /c "C:\Bat\BAK_ALL.bat"'), addr szComspec
invoke CreateProcess, 0, addr szAppname, 0, 0, FALSE, \
NORMAL_PRIORITY_CLASS, 0, 0, addr StartupInfo, addr ProcessInfo

      invoke WaitForSingleObject, ProcessInfo.hProcess, INFINITE

            .elseif wParam == DBT_DEVICEREMOVECOMPLETE


.endif
.elseif uMsg==WM_CLOSE

ret
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret

.endif
xor eax, eax
ret
WndProc endp

CheckDriveG proc    uses ebx esi edi ebp    DriveList: DWORD

            mov   eax, DriveList
            bt eax, 6 ; check if bit 6 is set (Drive G:)
            ret
           
CheckDriveG endp

CheckDriveH proc    uses ebx esi edi ebp    DriveList: DWORD

            mov   eax, DriveList
            bt eax, 7 ; check if bit 7 is set (Drive H:)
            ret
           
CheckDriveH endp

end USBWatcher

Have a great day,
                         Andy

jj2007

Even MasmBasic has no GOTO, Andy ;-)

ragdog

Hi Andy

I have your send already a source for detect a usb drive oder if disconnect

Detect USB device insertion:
http://www.winasm.net/forum/index.php?showtopic=3472

Magnum

Yes, but I have to modify it for a new situation.

Have a great day,
                         Andy

Magnum

Ollydbg will load this, but won't run it.

I need to see if my code is properly detecting whether a drive has as a  "backup" directory.


invoke  GetLogicalDrives        ; get drives
invoke  CheckDriveG, eax        ; check if drive G: is available

        push eax ; save for later use

        .if     (eax == 7Ch) ;match3

           ; check if G:drive has a Backup directory
           ; G: may be the printer drive
           
           invoke GetFileAttributes,offset G_USB_Directory

        .IF (EAX == 0FFFFFFFFh) ;match4

          jmp check_4_H_Drive 

        .ENDIF                  ;match3 
                 
        .endif                  ;match4

check_4_H_Drive: ; bit 7 is H: drive

         pop eax

         .if (eax == 0F8h)   

         invoke GetFileAttributes,offset H_USB_Directory
         
        .IF (EAX == 0FFFFFFFFh)

        int 3

        invoke  ExitProcess, 0

        .endif
        .endif
continue:

Have a great day,
                         Andy

oex

bsf eax, [ebx].dbcv_unitmask
add eax, 65
mov esi, chr$("X:\backup\.")
mov [esi], al
invoke exist, esi

There is a slight error in this.... "Although the dbcv_unitmask member may specify more than one volume in any message".... I *think* this means a partitioned drive....

It also says "this does not guarantee that only one message is generated for a specified event".... ie I guess this might mean that your code could execute twice....

http://msdn.microsoft.com/en-us/library/aa363249(v=vs.85).aspx
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv