Drive Detection

Discussion in 'Questions (Windows Mobile)' started by RandomCoder, Jun 25, 2008.

  1. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Hi guys,

    Been away for a while but just gettting back into some programming again.
    Found a little tool for copying files and folders called ROBOCOPY, like the old DOS XCOPY but better (see here... Robocopy), and I'm wanting to automate this with B4PPC.

    What I would like to know is if it is possible to detect when a USB drive has been plugged in?

    The best I have come up with is to run a DirExist("MyBackup.etc") periodically to check if the USB drive is present but this is very hit and miss.
    I'd much rather have some way of detecting that the drive has just been plugged in - is it possible?

    Regards,
    RandomCoder
     
  2. agraham

    agraham Expert Licensed User

    This uses my (unpublished) Window Message libray but it is based on the same code as Dimitris used for dzEventsMagic. Make a dzEventMagic object called WMevent and change the event name to WMevent_MagicEvent.

    Note that you get multiple messages, hence the check for a time window. You will have to track whether the device is present or not. Also note that you may need a delay after getting the event before the device is visible. You will have to play with this for yourself.

    This event may happen for other things than a USB device. This works on my desktop for USB sticks and should work on a device with a USB port but for what range of devices I have no idea.


    Code:
    Sub Globals
       
    'Declare the global variables here.
       WM_DEVICECHANGE = 537 ' 0x0219
       TimeChanged = Now
    End Sub

    Sub App_Start
       Form1.Show
       WMevent.New1(
    "Form1"false)
       WMevent.Hook(WM_DEVICECHANGE)
    End Sub


    Sub Form1_Close
       WMevent.UnHook(WM_DEVICECHANGE)   
    End Sub

    Sub WMEvent_WmEvent
       msg = WMevent.wParam
       
    If msg = 7 AND Now > TimeChanged + cTicksPerSecond*3 Then
          TimeChanged = Now
          
    Msgbox("Device added or removed")
       
    End If
    End Sub
     
  3. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Agraham your amazing..... is there anything you don't know?

    I've not tried it yet but have every faith it will work.

    Thankyou very much,
    RandomCoder
     
  4. agraham

    agraham Expert Licensed User

    Further investigation shows that you only get one each of events with these wParam values on a change. There may still be a delay before the device is accessible. Again you need to change the Sub name to WMevent_MagicEvent for the dzEventsMagic library.


    Code:
    Sub WMEvent_WmEvent
       msg = WMevent.wParam
       
    Select msg
          
    Case 32772:   
             
    Msgbox("Device removed")
          
    Case 32768
             
    Msgbox("Device added")
       
    End Select   
    End Sub
     
  5. agraham

    agraham Expert Licensed User

    Lots, but I can Google quite well and of course have the background to interpret and use what I find :)
     
  6. agraham

    agraham Expert Licensed User

    I've played some more and it seems, on my Vista desktop at least, that the USB drive is available as soon the drive added message arrives. This is convenient as it will save doing some timing logic before testing if it is true for all systems.
     
  7. robmh

    robmh Member Licensed User

  8. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I like the twist on the ROBOCOPY name... Robscopy ;)

    If you wouldn't mind then it would be nice to see your source code as there are some things you have done different such as using a TreeView to select paths and also the ability to exclude files (not just folders).

    My app is intended to run slightly differently in that I'd like to have it running in the background all of the time and then detect when x amount of time has passed (set by the user) and trigger the ROBOCOPY function, if the destination is a USB device it will wait for the device to be connected.

    Attached is my source code so that you can see the direction I've taken. This is no where near finished and does not include running in the background which needs to be added now that Agraham and DZT have been so kind as to show me a way forward. Most of my programming is done during a 30 minute meal break at work :sign0148:
    I've commented out the SHELL comand and just use a label to display the arguments for the time being.

    Regards,
    RandomCoder
     

    Attached Files:

  9. robmh

    robmh Member Licensed User

    Hi RandomCoder

    Please find the source code attached.
     
  10. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Thanks rob :sign0060:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice