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

Have a great day,
                         Andy

qWord

it is simple but it works:
(user CTRL+SHIFT+Q to quit the program)
Quoteinclude masm32rt.inc

DBT_DEVICEARRIVAL    EQU 8000h
DBT_DEVTYP_VOLUME   EQU 2

DEV_BROADCAST_HDR struct
    dbch_size      DWORD ?
    dbch_devicetype   DWORD ?
    dbch_reserved   DWORD ?
DEV_BROADCAST_HDR ends
PDEV_BROADCAST_HDR typedef ptr DEV_BROADCAST_HDR
DEV_BROADCAST_VOLUME struct
    dbcv_size      DWORD   ?
    dbcv_devicetype DWORD   ?
    dbcv_reserved   DWORD   ?
    dbcv_unitmask   DWORD   ?
    dbcv_flags      WORD   ?
DEV_BROADCAST_VOLUME ends
PDEV_BROADCAST_VOLUME typedef ptr DEV_BROADCAST_VOLUME

WndProc proto hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
.data
    szCmd db 'C:\windows\system32\xcopy.exe "C:\masm32" "G:\backup" /E /Y',0
.data?
    hInstance   HINSTANCE   ?
    pcbWndProc   PVOID      ?
.code
main proc
LOCAL msg:MSG

    mov esi,rv(AddAtom,"idhk")
    invoke RegisterHotKey,0,esi,MOD_CONTROL or MOD_SHIFT,VK_Q
    mov edi,rv(CreateWindowEx,0,"button",0,0,-100,-100,10,10,0,0,hInstance,0)
    mov pcbWndProc,rv(SetWindowLong,edi,GWL_WNDPROC,OFFSET WndProc)

    .while 1
        invoke GetMessage,ADDR msg,0,0,0
        .break .if !eax || eax == -1 || (msg.message == WM_HOTKEY && msg.wParam == esi)
    .endw
   
    invoke ExitProcess,0
   
main endp

WndProc proc uses edi hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL sui:STARTUPINFO
LOCAL pi:PROCESS_INFORMATION

    .if uMsg == WM_DEVICECHANGE
        .if wParam == DBT_DEVICEARRIVAL
            mov edi,lParam
            .if [edi].DEV_BROADCAST_HDR.dbch_devicetype == DBT_DEVTYP_VOLUME
                .if ![edi].DEV_BROADCAST_VOLUME.dbcv_flags
                    mov eax,[edi].DEV_BROADCAST_VOLUME.dbcv_unitmask
                    bsf eax,eax
                    lea edx,[eax+'A']
                    .if edx == 'G'
                        invoke RtlZeroMemory,ADDR sui,sizeof sui
                        mov sui.cb,sizeof sui
                        fn CreateProcess,0,OFFSET szCmd,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR sui,ADDR pi
                        ;invoke WaitForSingleObject,pi.hProcess,INFINITE
                    .endif
                .endif
            .endif
        .endif
    .else
        invoke CallWindowProc,pcbWndProc,hWnd,uMsg,wParam,lParam
        ret
    .endif

    xor eax,eax
    ret   
   
WndProc endp
end main
FPU in a trice: SmplMath
It's that simple!

Magnum

I need it to run a batch file.

This isn't working.


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

.data
    szCmd db '"%s" /c "C:\Bat\BAK_ALL.bat"'

.data?
    hInstance   HINSTANCE   ?
    pcbWndProc   PVOID      ?

.code

main proc
LOCAL msg:MSG

    mov esi,rv(AddAtom,"idhk")
    invoke RegisterHotKey,0,esi,MOD_CONTROL or MOD_SHIFT,VK_Q
    mov edi,rv(CreateWindowEx,0,"button",0,0,-100,-100,10,10,0,0,hInstance,0)
    mov pcbWndProc,rv(SetWindowLong,edi,GWL_WNDPROC,OFFSET WndProc)

    .while 1
        invoke GetMessage,ADDR msg,0,0,0
        .break .if !eax || eax == -1 || (msg.message == WM_HOTKEY && msg.wParam == esi)
    .endw
   
    invoke ExitProcess,0
   
main endp

WndProc proc uses edi hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL sui:STARTUPINFO
LOCAL pi:PROCESS_INFORMATION

    .if uMsg == WM_DEVICECHANGE
        .if wParam == DBT_DEVICEARRIVAL
            mov edi,lParam
            .if [edi].DEV_BROADCAST_HDR.dbch_devicetype == DBT_DEVTYP_VOLUME
                .if ![edi].DEV_BROADCAST_VOLUME.dbcv_flags
                    mov eax,[edi].DEV_BROADCAST_VOLUME.dbcv_unitmask
                    bsf eax,eax
                    lea edx,[eax+'A']
                    .if edx == 'J' ; Drive letter
                        invoke RtlZeroMemory,ADDR sui,sizeof sui

Have a great day,
                         Andy

ragdog

invoke GetEnvironmentVariable, CTEXT('ComSpec'), addr szComspec, sizeof szComspec
invoke wsprintf, addr @Result, CTEXT('"%s" /c "bat.cmd"'), addr szComspec
invoke WinExec,addr @Result,SW_SHOW

Magnum

It would not work.

I decided to use some earlier code and just turn on my printer each time.

Have a great day,
                         Andy

qWord

Magnum,
you only need to change the commandline:

szCmd db 'C:\windows\system32\cmd.exe /C "C:\test.bat"',0
FPU in a trice: SmplMath
It's that simple!

dedndave

this may be helpful, too - at least while debugging your code
/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

by using /K, it is easier to tell if the shell is executed

Magnum

Thanks Qword.

It works perfectly now and I don't have to turn on my printer.

Have a great day,
                         Andy

Magnum

Completed project attached.
Have a great day,
                         Andy

Magnum

Not quite finished.

I need to add a check for drive g: to this code.

Do I add another .if after fnCreateProcess or after the .endif ?


    .if uMsg == WM_DEVICECHANGE
        .if wParam == DBT_DEVICEARRIVAL
            mov edi,lParam
            .if [edi].DEV_BROADCAST_HDR.dbch_devicetype == DBT_DEVTYP_VOLUME
                .if ![edi].DEV_BROADCAST_VOLUME.dbcv_flags
                    mov eax,[edi].DEV_BROADCAST_VOLUME.dbcv_unitmask
                    bsf eax,eax
                    lea edx,[eax+'A']
                    .if edx == 'J'
                        invoke RtlZeroMemory,ADDR sui,sizeof sui
                        mov sui.cb,sizeof sui
                        fn CreateProcess,0,OFFSET szCmd,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR sui,ADDR pi
                    .endif
                .endif
            .endif
        .endif
    .else

Have a great day,
                         Andy

oex

                    .if edx == 'J' || edx == 'G'
                        invoke RtlZeroMemory,ADDR sui,sizeof sui
                        mov sui.cb,sizeof sui
                        fn CreateProcess,0,OFFSET szCmd,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR sui,ADDR pi
                    .endif


or

                    .if edx == 'J'
                        invoke RtlZeroMemory,ADDR sui,sizeof sui
                        mov sui.cb,sizeof sui
                        fn CreateProcess,0,OFFSET szCmd,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR sui,ADDR pi
                    .elseif edx == 'G'
; A different condition here....
                    .endif
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Magnum

Thanks oex.

Is || the same as an OR statement ?

I did a search for || and OR but didn't find anything.



Have a great day,
                         Andy

qWord

FPU in a trice: SmplMath
It's that simple!

oex

Quote from: Magnum on March 31, 2011, 11:06:57 PM
Is || the same as an OR statement ?

Yes but I am unaware if in ASM they operate at different precedence levels....
http://www.php.net/manual/en/language.operators.precedence.php

Also note that
.if edx == 'J' || edx == 'G'
This is 1 condition.... This will check both and execute if 1 matches....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv