5

Is it possible to make a program auto run (execute) when the USB it is stored on is plugged into a computer

I don't think this is possible due to the searching I have looked at on the Internet and also the security risk such coding would have

E.g. I have a simple countdown timer (10 to 1) programmed in C++ (complied on windows), when the USB (which the .exe file is stored on) is plugged into a computer the timer will start without me executing it manually.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Dan1676
  • 1,685
  • 8
  • 24
  • 35
  • 2
    It's software - pretty much anything is possible. In this case you might need extra software installed to help you out. – Carl Norum Feb 26 '12 at 16:32
  • @CarlNorum: Indeed, but in that case why wouldn't you let the _installed_ software perform the countdown? – MSalters Feb 27 '12 at 10:47

4 Answers4

4

You can create an Autorun.inf file and place it on the USB drive. Windows will read this file when the drive is connected. In the file you can specify the name of the program you'd like to run, as well as icons, etc.

You cannot force the program to run when the USB drive is inserted. That's up to the security policy of the operating system, and most will at least prompt the user "Do you want to run 'Setup.exe' from this drive?" or something like that.

SoapBox
  • 20,457
  • 3
  • 51
  • 87
  • 1
    as far as I understand, `Autorun.inf` is ignored for non-optical drives (or at the very least many removable media types such as USB storage). The way many companies work around this is by having the USB device actually report to the OS as two devices, one USB storage and one optical drive and put the autorun on the virtual optical drive. – Evan Teran Feb 26 '12 at 16:49
  • 1
    It strongly depends on Windows version and settings; I estimate that less than 50% of Windows PCs still run `Autorun.inf`. – MSalters Feb 27 '12 at 10:45
  • Any (windows- or macos-included) ways without the autorun.inf stored on the exact usb? Anything pc-sided that executes when certain device gets plugged in, just like you can set up to open folders when any usb device gets connected? (Because running autorun files from any usb is always tricky in terms of safety, as we all know) – LuckyLuke Skywalker Feb 17 '22 at 19:46
1

By default it's not possible, since Windows puts users in control of their computers as much as possible—if they don't want autorun enabled, it will not be possible to have a program executed automatically.

You could write a helper program that runs in the background (possibly as a service) that sleeps most of the time, waiting for a USB device to be plugged in. There might be a way to receive notifications of this, or you might just have to poll it periodically, I don't know. Once it detects a USB device, it can then of course do whatever it wants (CreateProcess etc.).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • See [this question](http://stackoverflow.com/questions/4078909/detecting-usb-insertion-removal-events-in-windows-using-c) for details on receiving such notifications. – Ben Voigt Feb 26 '12 at 16:40
  • 3
    I'm tempted to edit that answer to: "since Windows puts users in conflict with their computers as much as possible" :) – user2677034 Jan 01 '22 at 20:51
0

Since Windows 7, the autorun.inf file is ignored when it comes to USB devices. U3 technology allows a thumb drive to be treated as a CD/DVD which might be a possible solution.

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
0

I think you can do so using system(), if you are familiar with disktype command in cmd, then I think you can figure things out. All what you can do in command prompt you can also do in c++ using system(). But it will consume alot of memory space