This code identifies and monitors removable drives connected to a...
This code identifies and monitors removable drives connected to a Windows system and writes their information into a configuration .inc
file. Here's a breakdown of what it does:
1. Header Files
It includes various headers for Windows API and standard utilities:
windows.h
,fileapi.h
: For interacting with Windows functionality like drive information and handling messages.iostream
,fstream
, etc.: For input/output and string manipulation.
2. Constants
Defines INC_FILE
as the path and name for the output configuration file where removable drive data will be stored: Storage Edits\drive.inc.
3. GetRemovableDrives
Function
- This function detects all removable drives currently connected to the system.
- It uses the
GetLogicalDrives
function to get a bitmask of available drives (A:
toZ:
), checks whether each one is removable usingGetDriveTypeA
, and retrieves its volume label usingGetVolumeInformationA
. - Any spaces in the volume name are replaced with underscores.
- The result is a list (
std::vector
) of pairs containing the volume name and drive letter (e.g.,{"MyUSBDrive", "E:"}
).
4. WriteDriveINC
Function
- Writes the list of removable drives to the
drive.inc
file. - The format of the file is:
[Variables] DriveName1=DriveLetter1 DriveName2=DriveLetter2
- Any previous contents of the file are replaced (
std::ios::trunc
mode).
5. WndProc
Function
- This is a callback function (window procedure) used to handle Windows system messages.
- It monitors device changes using the
WM_DEVICECHANGE
message. Specifically:- When a device is added (
DBT_DEVICEARRIVAL
) or removed (DBT_DEVICEREMOVECOMPLETE
), the program updates thedrive.inc
file by callingGetRemovableDrives
and writing the new list of drives usingWriteDriveINC
.
- When a device is added (
6. main
Function
The main
function does the following:
- Ensures the output folder (
Storage Edits
) exists by callingCreateDirectoryA
. - Writes the initial list of removable drives to the
drive.inc
file usingWriteDriveINC
. - Creates a hidden window to receive system messages, specifically those for detecting device changes:
- A custom window class is registered using
RegisterClassA
, and a window is created withCreateWindowA
. The window does not display anything on the screen but allows the program to receive system device notifications.
- A custom window class is registered using
- Runs an infinite message-processing loop (
GetMessageA
) to handle Windows events, ensuring the program updates thedrive.inc
file when changes (like USB drive insertion/removal) occur.
Purpose
This program continuously monitors removable (e.g., USB) drives. It creates or updates a configuration file (drive.inc
) that lists all currently connected removable drives with their names and letters.
The following actions occur:
- On startup: Writes the initial list of removable drives to
drive.inc
. - When USB devices are plugged or unplugged: Detects the change, updates the drive list, and rewrites
drive.inc
.
Key Features
- Dynamic Drive Monitoring: Automatically detects when drives are added or removed.
- Outputs Configuration File: Runnable in systems where a consumable
.inc
file with settings (e.g., drive names and letters) is needed. - Hidden Execution: The program runs silently, interacting with the system without additional user interface.
Use Case
This code could be useful in scenarios where a program or system component requires real-time information about connected USB drives, such as:
- Synchronizing files to/from specific drives.
- Logging drive usage.
- Using removable drives for device authentication or configuration.
Example drive.inc
Output
After detecting a USB drive named "MY_USB" assigned to drive "E:", the drive.inc
file may look like this:
[Variables]
MY_USB=E:
If another drive is plugged in, such as "AnotherDrive" at "F:", it is updated to:
[Variables]
MY_USB=E:
AnotherDrive=F: