Tell me, is it possible to make such an application and place it on an external disk that automatically started when this external disk was connected?

The program itself will from time to time to calculate the disk space and send it to the server.

Also, when disconnecting the railway (as safe and pulling out), no errors should arise.

  • one
    support.microsoft.com/en-us/kb/818804 Autorun.inf will help. was interested in? - Senior Pomidor
  • 2
    then your program should copy itself to the temp directory, from there start yourself. and then in a separate stream you look at the state of the disk, and you do your work with another stream. and when the disk is disconnected, you will send it to the server - Senior Pomidor
  • four
    @ iluxa1810 autorun on flash cards is disabled in more or less new versions of win due to active use by virus writers. - PashaPash
  • four
    @ iluxa1810: I think the service is a more painless solution. If you allow autorun on any of the disks, it will be a hole for virus penetration. - VladD
  • 2
    It would be good to explain the scenarios in which the written program is supposed to be used. If "I am a sysadmin, I can arrange the program on the required computers by the policy and then monitor the numbers on the server" - this is one thing (then the windows service is the thing if you decide not to get involved with ready-made monitoring solutions like SCOM, zabbix, etc.). If "I will give this flash drive to inexperienced users, it should start up when it is connected, send information to the server, and stop correctly when uncoupling" - then you need an application, not a service; the ability to be installed from the current non-Fairy ..... - AK

1 answer 1

Do you mean launching a program from a hard disk when you connect an external disk on the same principle as antiviruses start scanning removable media?

Then you need to register your program as an event handler associated with devices (for more information about them, see the article " Device Events " on MSDN). In the same place, on MSDN, there is an example of registration of this handler. Below I will give a brief squeeze out of it:

  1. We HidD_GetHidGuid() GUID that identifies removable devices ( HidD_GetHidGuid() function);
  2. DEV_BROADCAST_DEVICEINTERFACE structure and pass it to the RegisterDeviceNotification function. Everything, the handler is registered.
  3. We process window event WM_DEVICECHANGE . The information we need is passed through lParam , in the form of a pointer to the DEV_BROADCAST_DEVICEINTERFACE structure, and wParam , in the form of an event type code. The code of interest to us (device insertion) is DBT_DEVICEARRIVAL .
  • Is it possible to somehow find out if the disk is idle or is something written / read on it? - iluxa1810
  • And why is it necessary to know? Probably you are solving the problem from the wrong end. - ߊߚߤߘ
  • Topicaster explicitly indicated that the program should be located on the disk itself , and not just somewhere on the machine to which this disk is connected. Those. He asked about autorun, and not just about the device connection detection. - PashaPash
  • @iluxa1810 проще в таком случае обойтись вовсе без автозапуска - просто установить на компьютер службу, которая будет диск отслеживать. – VladD @iluxa1810 проще в таком случае обойтись вовсе без автозапуска - просто установить на компьютер службу, которая будет диск отслеживать. – VladD - @VladD, хорошо, убедили... - ߊߚߤߘ
  • @ iluxa1810, once again: for what purpose should it be waiting for idle disk? You need to calculate the disk space - this is a matter of reading the file attributes (a few bytes from the system table), and not their contents. The current load of the disk affects this weakly (especially considering that some of the attributes are most likely already cached in RAM and do not require accessing the disk). - ߊߚߤߘ