-
[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:, ...)
-
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.
-
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
-
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
-
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 :)
-
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
-
Re: Launching program on USB memory drive insertion
but he could make that on it
-
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...
-
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
-
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:
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array("****")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskRootDirectory", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "GroupComponent: " & objItem.GroupComponent
WScript.Echo "PartComponent: " & objItem.PartComponent
WScript.Echo
Next
Next
-
Re: Launching program on USB memory drive insertion
what about doin a autorun.inf?
-
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.
-
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...
-
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.
-
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. :)
-
Re: Launching program on USB memory drive insertion
I think scriptomatic 1 ran under w98. look for it.
-
Re: Launching program on USB memory drive insertion
did you look into the autorun option for your usb stick? it should be available
-
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
-
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
-
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.
-
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
-
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
-
Re: Launching program on USB memory drive insertion
Quote:
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
-
Re: Launching program on USB memory drive insertion
Quote:
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
-
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.
-
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".
-
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
-
Re: Launching program on USB memory drive insertion
i thought that you knew one way or the other. :rolleyes:
-
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:
Private Sub SysInfo1_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Dim strFile As String
Debug.Print "DeviceType = " & DeviceType
Debug.Print "DeviceID = " & DeviceID
Debug.Print "DeviceName = " & DeviceName
Debug.Print "DeviceData = " & DeviceData
strFile = GetDriveLetter(DeviceID) & ":\Project1.exe"
If Dir$(strFile) <> vbNullString Then
Shell strFile, vbNormalFocus
Else
MsgBox "Project1.exe not found!"
End If
End Sub
Private Function GetDriveLetter(sDrv As Long) As String
Dim cDrv As Integer
cDrv = 65 'Letter "A" for drive A
Do Until (sDrv And 1) = 1
sDrv = sDrv / 2
cDrv = cDrv + 1
Loop
GetDriveLetter = Chr$(cDrv)
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.
-
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.
-
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
-
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?
-
Re: Launching program on USB memory drive insertion
try changing the current drive (path) for your program
-
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!
-
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.
-
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 ?