Results 1 to 36 of 36

Thread: [RESOLVED] Launching program on USB memory drive insertion

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Resolved [RESOLVED] Launching program on USB memory drive insertion

    This one could be tricky.

    I recently got one of those shiny, new USB memory drive things and I'm wondering what exactly I can do with it. Then, I got an idea, but I need help putting it together.

    Whenever I insert the USB drive in a computer, I would like to have a program that I coded in VB6 and located ON the drive launch itself. Ideally, the thing would be completely independant and not rely on anything else to start. However, a solution using a small program already present and running on the computer itself to detect the device and start the main application could work, too.

    Also, is there a way to find out which letter drive the USB device was assigned to? (for example, is it E:, or F:, ...)
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    you could just check for a file on the drive, or use the api to check if a folder exists. i saw it yesterday.

  3. #3
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Launching program on USB memory drive insertion

    Never played with one of the new USB drives so my guess is:

    If they require a driver, you might be able to modify the driver so that when the USB drive is recognized it goes out and executes your program. Will require assembly and C++ and ALOT of effort.

    Another thought, might be to monitor the registry for new hardware (assumption here that USB drive insertion triggers a registry entry
    when the hardware is installed), and then run a bat file based on this entry to execute your program. THis however would require a timer and would be resource intensive.

    Other than above, I can't see how the USB drive insertion will be recognized for program execution.

    David

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    it automatically maps to a drive when it connects. the drive is not readable until it does. it works the same way for a camera memory stick. you insert the stick, and double-click on the drive. the problem is that there is more than one drive that you can click on (on my buddy's machine there were 3) for his camera, and a stick reader that reads two different kinds of stick

  5. #5
    Lively Member
    Join Date
    Sep 2003
    Location
    Infront of my PC
    Posts
    96

    Re: Launching program on USB memory drive insertion

    i rekon u should do this


    make a program that runs all the time
    this program checks for a file in the say F G and H drives for a file called say usb.dll when it finds this file it launches the program
    Creator of Winamp To Nick
    2nd Prize STS Game Programming 04
    Lead Programmer: Squandy Productions


    Current Project: Winamp to Sig
    if i say something good please give me some reputation!

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    I don't think the dll is there, he'd have to check for his app.exe

  7. #7
    Lively Member
    Join Date
    Sep 2003
    Location
    Infront of my PC
    Posts
    96

    Re: Launching program on USB memory drive insertion

    but he could make that on it
    Creator of Winamp To Nick
    2nd Prize STS Game Programming 04
    Lead Programmer: Squandy Productions


    Current Project: Winamp to Sig
    if i say something good please give me some reputation!

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by dglienna
    I don't think the dll is there, he'd have to check for his app.exe
    He meant that I could put such a file on the USB drive.


    Quote Originally Posted by liverpool_rule
    i rekon u should do this


    make a program that runs all the time
    this program checks for a file in the say F G and H drives for a file called say usb.dll when it finds this file it launches the program
    That's a possibility, but the problem is that it's pretty ressource intensive to check on, say, three drive letters to see whenever one happens to receive the USB device. Besides, that wouldn't work so well on a computer where someone would have a lot of hard drives / partitions: at which letter to you stop checking? Or do you check for all of them up to Z:? It's way overkill...
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    many usb sticks can autorun like a cd, but that would have to be enabled on any computer that you plug into,

    check the instructions with your device and search google for more information

    rgds pete

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    This doesn't register for my floppy, as there is nothing in it. It'd could work for the memory stick, I'd imagine. Change **** to your computername.
    This is vbs code.


    VB Code:
    1. On Error Resume Next
    2.  
    3. Const wbemFlagReturnImmediately = &h10
    4. Const wbemFlagForwardOnly = &h20
    5.  
    6. arrComputers = Array("****")
    7. For Each strComputer In arrComputers
    8.    WScript.Echo
    9.    WScript.Echo "=========================================="
    10.    WScript.Echo "Computer: " & strComputer
    11.    WScript.Echo "=========================================="
    12.  
    13.    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    14.    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskRootDirectory", "WQL", _
    15.                                           wbemFlagReturnImmediately + wbemFlagForwardOnly)
    16.  
    17.    For Each objItem In colItems
    18.       WScript.Echo "GroupComponent: " & objItem.GroupComponent
    19.       WScript.Echo "PartComponent: " & objItem.PartComponent
    20.       WScript.Echo
    21.    Next
    22. Next

  11. #11
    Lively Member
    Join Date
    Sep 2003
    Location
    Infront of my PC
    Posts
    96

    Re: Launching program on USB memory drive insertion

    what about doin a autorun.inf?
    Creator of Winamp To Nick
    2nd Prize STS Game Programming 04
    Lead Programmer: Squandy Productions


    Current Project: Winamp to Sig
    if i say something good please give me some reputation!

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    It would still have to be turned on for the device, whereas a program could just poll for the specific file, and then open it. It would only open when the program is searching for it.

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by dglienna
    This doesn't register for my floppy, as there is nothing in it. It'd could work for the memory stick, I'd imagine. Change **** to your computername.
    This is vbs code.
    *snip*
    Unfortunately, it doesn't do anything beyond displaying my computer's name. Any ideas why?


    Quote Originally Posted by liverpool_rule
    what about doin a autorun.inf?
    I just tested that. I can change the displayed icon in My Computer, so the file is recognized. However, with the default settings, it doesn't launch the executable set with the open= command. I apparently can't find either where I could activate the auto run to open my program...
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    it registered my dvd/cd and my hard disk, don't know why it didn't work for you. as a matter of fact, i don't think it reported for my mapped drive.

    download scriptomatic2 from msdn. it writes these scripts for you. you just have to find the one that you want. the first one didn't run for me, but this one seems to work well.

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by dglienna
    it registered my dvd/cd and my hard disk, don't know why it didn't work for you. as a matter of fact, i don't think it reported for my mapped drive.
    I just found out why: without thinking, I only tested the script under Windows 98... It doesn't return anything there, but it worked fine under Windows XP and reported all of my drives (including the USB drive). I suppose this could be an option if I don't find anything else... Autorun would still be nice.
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    I think scriptomatic 1 ran under w98. look for it.

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    did you look into the autorun option for your usb stick? it should be available

  18. #18
    Member
    Join Date
    Jan 2005
    Posts
    56

    Re: Launching program on USB memory drive insertion

    Yeah it's true autorun would have to be enabled on the computer you plug it into, but if you made that program that polls for the drive then THAT would have to be installed on any computer you want to use it on anyway.

    Autorun is the way to go. Most people have it enabled anyway.

    -Mike

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    you might have to change registry settings for computer to autodetect removable drive even when auto run is on,

    auto run can be set to run off any drive type including remote drives, but by default is only set to fixed drives, cdroms and ram drives and drives with no root directory

    value 95hex


    this matter is considered to be a security issue as it can allow undesirable programs to run

    rgds pete

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    not enabled for anything except CD by default. floppy disks don't run by default if they have autoexec.bat on it.

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    autoexec.bat is only on bootup from that device, but according to microsoft, you can make floppys autorun, by putting an autorun.inf on the disk and changing the registry value to include removable drives, in addition to the ones i listed before, the same setting should encompass usb removable drives

    pete

  22. #22
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by westconn1
    you might have to change registry settings for computer to autodetect removable drive even when auto run is on
    See http://www.ashzfall.com/products/aut...runfloppy.html
    This tells you exactly what registry setting to change.

    I just tried it on a Win98se system, with autorun.inf on a USB pen drive.
    When I plug it in the autorun.inf updates the device's icon in windows explorer, but it doesn't run the exe.
    If I right-click on the device and select AutoPlay, it then runs the exe.

    Autorun.inf
    Code:
    [autorun]
    open=F:\path\file.exe
    icon=F:\path\file.ico
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  23. #23
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    I just tried it on a Win98se system, with autorun.inf on a USB pen drive
    well you had more of a go than i did my icon would show up, but the exe would not run at all, unless i double clicked the exe file
    windows XP sp1

    looks like microsoft hasn't updated there website

    well there you go, scratch that one

    pete

  24. #24
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Cool Re: Launching program on USB memory drive insertion

    Originally Posted by DaveBo

    Autorun.inf
    [autorun]
    open=F:\path\file.exe
    icon=F:\path\file.ico
    I don't like the F:\ path info. The USB drive doesn't know what drive letter it will be on.

    I have seen:
    Code:
    [AutoRun]
    OPEN=setup.exe
    Now you've got me interested...

    EDIT1: Oh well, my USB attempt didn't work
    Code:
    In autorun.inf
    [AutoRun]
    OPEN=test.bat
    
    
    In test.bat
    echo hello
    pause
    But I haven't tried this: Check out this link similar to DaveBo's link
    http://www.moonvalley.com/products/rwavdc/enable.htm
    Page down for "How To Enable Autorun for Other Removable Media"

    EDIT2: Oh well, it didn't work for me on Windows 2000
    Last edited by Phenix; Jan 25th, 2005 at 05:00 PM. Reason: Added failed attempts and new link
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    i had an old dos program that would start the drive spinning as soon as you put it in. also buffered read and write access. i forget how i knew when it had finished, as the drive never stopped spinning.

  26. #26

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by DaveBo
    I just tried it on a Win98se system, with autorun.inf on a USB pen drive.
    When I plug it in the autorun.inf updates the device's icon in windows explorer, but it doesn't run the exe.
    If I right-click on the device and select AutoPlay, it then runs the exe.
    My USB memory drive is on autorun (used TweakUI to make sure it was), and it's not going farther than last time: under Windows XP SP2, the icon shows but the right-click to AutoPlay doesn't work. It only displays WinXP's action screen instead, where you can select which action to perform (and my application isn't listed there). Tricky thing...

    On a related note: the autorun doesn't work at all under Windows 98: even the custom icon doesn't display in "My Computer".
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  27. #27
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    i dare say this is a matter that has been addressed by microsoft, in later versions of windows, xp etc, so that it can't autorun from a stick drive as a security issue.

    back to polling the drives, but you have to install a small program on each machine first and have it load at startup

    rgds pete

  28. #28
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Launching program on USB memory drive insertion

    i thought that you knew one way or the other.

  29. #29
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: Launching program on USB memory drive insertion

    hi, i just figured this might help solve your problem. i tried using the Microsoft Sysinfo ocx (you should add this component on your project). it has a DeviceArrival event which fires whenever a new device is inserted like USB Flash drives. i made this simple application. here's what you should do:

    1. Place the vb application you wish to auto run in your flash drive. to make things simple, just place it in the root directory of your usb drive.
    2. make another application. this application will the one responsible for running the application on your usb drive.
    3. start a new vb project and add this component : "Microsoft SysInfo Control 6.0"
    4. add the following code on your form:

    VB Code:
    1. Private Sub SysInfo1_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    2.  
    3.    Dim strFile As String
    4.    
    5.    Debug.Print "DeviceType = " & DeviceType
    6.    Debug.Print "DeviceID = " & DeviceID
    7.    Debug.Print "DeviceName = " & DeviceName
    8.    Debug.Print "DeviceData = " & DeviceData
    9.    
    10.    strFile = GetDriveLetter(DeviceID) & ":\Project1.exe"
    11.    
    12.    If Dir$(strFile) <> vbNullString Then
    13.       Shell strFile, vbNormalFocus
    14.    Else
    15.       MsgBox "Project1.exe not found!"
    16.    End If
    17. End Sub
    18.  
    19. Private Function GetDriveLetter(sDrv As Long) As String
    20.    Dim cDrv As Integer
    21.    
    22.    cDrv = 65            'Letter "A" for drive A
    23.    
    24.    Do Until (sDrv And 1) = 1
    25.       sDrv = sDrv / 2
    26.       cDrv = cDrv + 1
    27.    Loop
    28.    GetDriveLetter = Chr$(cDrv)
    29. End Function

    5. run this program and then try to insert your flash drive.
    6. in this example, i have another vb program named Project1.exe residing in my usb flash drive. once the DeviceArrival is fired, this application will executed

    you can also use subclassing to detect when a new device is inserted. there some examples in psc about that. in this example i use the SysInfo control to make things easy

    hope this helps.
    Last edited by guyjasper; Jul 13th, 2005 at 03:12 AM.

  30. #30
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Launching program on USB memory drive insertion

    Very good!

    This works well on my Win2K

    but, I just tried on a Win98 laptop, and, for some strange reason the DeviceArrival event is being triggered 4 times when I insert the USB pen drive.
    The 1st three times it reports an apparently bogus DeviceID (long negative numbers) & DeviceType=1,
    and the 4th time it returns the correct one and DeviceType=2.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  31. #31
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Launching program on USB memory drive insertion

    Found answer at http://msdn.microsoft.com/library/de...icearrival.asp

    For logical volumes (drives) DeviceType=2
    i.e. in the code above you may want to ignore if DeviceType<>2

    DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  32. #32

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    I'm still poking around with this for fun, and I've run into a problem. I went with the latest suggestion, which has a detection program always running in the background. When it detects my USB drive, it launches the app on the drive using "Shell".

    However, when the time comes to remove the drive using Windows' systray icon to do so, it refuses to do it even if the launched app has been shutdown: the detection program blocks the removal. If I close that, I can remove the drive properly.

    Does the Shell command links the launched application to the running program, even after the app has been closed? If so, how can I break that link?
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  33. #33
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Launching program on USB memory drive insertion

    try changing the current drive (path) for your program
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  34. #34

    Thread Starter
    Member
    Join Date
    Sep 2001
    Location
    Quebec, Canada
    Posts
    43

    Re: Launching program on USB memory drive insertion

    Ahh, that's it! Thanks! To anyone who might read this, here's a more in-depth explanation:

    Within my detection program, a routine is called whenever a new USB device is detected. In that code, I first check to see if the application to launch is present using Dir$ (pathToApp) (after all, it could be a different drive than the one expected). If it's indeed available, I call it using Shell. However, that call to Dir$ () kept a pointer of sort to the USB drive, which prevented it's removal. As a fix, once the app is launched properly, I do a simple call to Dir$ ("C:\").

    I guess I've done everything I wanted with this topic. Resolved!
    - Dr. Cossack

    Interordi.com: Internet tools, programming, games, and more!

  35. #35
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Talking Re: [RESOLVED] Launching program on USB memory drive insertion

    I still think that autorun is the best way to go. I've been having lots of fun with autorun files recently and if you use a relative file path, you should have no problems. Also, you may wish to look into the "shell\verb\command=Filename.exe" and "shell\readit\command=Filename.exe" commands found at MSDN.
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  36. #36
    New Member
    Join Date
    Apr 2008
    Posts
    1

    Re: Launching program on USB memory drive insertion

    Quote Originally Posted by Dr. Cossack

    I guess I've done everything I wanted with this topic. Resolved!
    I know this is long since your last post, and you'll likely not see this, but it may be resolved for you, but I have the same problem, and know nothing about programming, so your explanation is Greek to me.

    Could you post and email me that you've done it, a complete script that will autorun a specific file. I can figure out the file name to change part, but I can't write the script. Or, just email me the script at:

    [email protected]

    Gmail doesn't allow executables as attachments, so you can use the workaround I figured out. Change the file's extension to '.tst', put it in a folder with some other files (anything will do), and .rar the file. If you do that, Gmail doesn't detect the executable. I don't know why, but I just stumbled across it by using the .tst (meaning 'test') when I was trying to find a workaround.

    I'd really like to have that script. Please ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width