I would like to modify this so it would check every 5 seconds to see if I have plugged in a thumb drive.
Then it could copy some backup files to it.

I realize that it would have to stay in memory. As long as it did not use a lot of resouces.

How could I do that?

Thanks.



invoke CreateFile,CTEXT("f:\result.txt"), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, \
0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0

      .IF (eax==INVALID_HANDLE_VALUE)
invoke MessageBox, 0, CTEXT("Thumb drive not plugged in."), CTEXT(" "), MB_OK
invoke ExitProcess, -1
.else
            invoke MessageBox, 0, CTEXT("USB drive is F:"), CTEXT(" "), MB_OK

    .ENDIF
Posted on 2009-11-12 22:53:16 by skywalker
Polling sucks - I think this is what you want to look at. Also, hardcoding drive letters won't work, as USB devices don't always get assigned the same letter.

I realize that it would have to stay in memory. As long as it did not use a lot of resouces.

How could I do that?
Simple - don't terminate your app :)

You can have it running silently in the background without a GUI, or you can add a tray notification icon, or whatever - there's plenty of choices.
Posted on 2009-11-13 01:19:49 by f0dder
Can't thumb drives have an autorun ? Why not have your app get started with the thumb drive and back up data to it ?
Posted on 2009-11-13 06:38:23 by JimmyClif

Can't thumb drives have an autorun ? Why not have your app get started with the thumb drive and back up data to it ?
They can, but autorun has been disabled on anything but optical media on recent windows versions for security issues.
Posted on 2009-11-13 08:24:55 by f0dder

They can, but autorun has been disabled on anything but optical media on recent windows versions for security issues.


Good call ! Was also a tad annoying.

@Skywalker: What do you need to backup? As f0dder said polling isn't the way to go and drive letters may change. I use a combination of MSBackup and Windows Live Sync (formerly foldershare) to create and store it remotely.

I believe there are also online backup services which could provide some services for you : https://mozy.com/ Afaik they set up a desktop folder client thingy and backs up whatever changes. 2GB Free for home users.
Posted on 2009-11-13 11:07:38 by JimmyClif
no need to poll, its the wrong way to do it it...
simply handle the WM_DEVICECHANGE message

http://msdn.microsoft.com/en-us/library/aa363480%28VS.85%29.aspx

this would be the proper way to do it, using less resources too :)

once you get the message you want, then do the checking

note. that when you get the message, the system may not have fully initialised the unit, so wait a little
(i usually wait ~30 seconds) and then do the check, set a flag then to check again (in a reasonable
amount of time), and unset this flag if you get the removal message (you might also want to make some
of the flags global, so you can check them in the routine doing the backup.. )

Posted on 2009-11-14 08:50:31 by evlncrn8


They can, but autorun has been disabled on anything but optical media on recent windows versions for security issues.


Good call ! Was also a tad annoying.

@Skywalker: What do you need to backup? As f0dder said polling isn't the way to go and drive letters may change. I use a combination of MSBackup and Windows Live Sync (formerly foldershare) to create and store it remotely.

I believe there are also online backup services which could provide some services for you : https://mozy.com/ Afaik they set up a desktop folder client thingy and backs up whatever changes. 2GB Free for home users.



My main backup is to a 2nd hard drive, but it would be nice to be able to detect when I have plugged my thumbdrive in and have files copied to it without having to run a batch file to do it.

Andy
Posted on 2009-11-14 16:23:37 by skywalker
Thanks.

It looks like I will need to use both WM_DeviceChange and DBT_DEVICEARRIVAL.

Andy
Posted on 2009-11-14 16:31:07 by skywalker

no need to poll, its the wrong way to do it it...
simply handle the WM_DEVICECHANGE message


Nice ! Once you know the answer it all makes sense :)
Posted on 2009-11-14 18:14:10 by JimmyClif

Polling sucks - I think this is what you want to look at. Also, hardcoding drive letters won't work, as USB devices don't always get assigned the same letter.

This has been truly helpful!  I did not know about this, and didn't know I needed it, until I saw this.  I have now used it in two projects to replace a less efficient method.  Thank you for this post!

Another tool for my toolbelt.

farrier
Posted on 2009-11-14 21:17:17 by farrier


Polling sucks - I think this is what you want to look at. Also, hardcoding drive letters won't work, as USB devices don't always get assigned the same letter.

This has been truly helpful!  I did not know about this, and didn't know I needed it, until I saw this.  I have now used it in two projects to replace a less efficient method.  Thank you for this post!
If you have access to a bunch of different Windows versions (I'm thinking especially Win9x and Win2k-or-older NT versions) it would be pretty helpful if you could do some testing to see where it works - I lost my VM images for older Windows versions, and I fear that WM_DeviceChange might only be XP-and-later.
Posted on 2009-11-15 10:24:29 by f0dder
its 2k or later :) msdn stated it

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Posted on 2009-11-15 11:45:15 by evlncrn8

its 2k or later :) msdn stated it

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
MSDN has dropped availability info for anything older than Win2k (no Win9x or NT4 info), so you can't really depend on MSDN for that information any longer... unless you have an older versions of MSDN/PlatformSDK offline :)
Posted on 2009-11-16 01:47:52 by f0dder