Jump to content

AutoIt Scripting: Drive Event Monitoring and Automation UDF


Numeric1
 Share

Recommended Posts


AutoIt Drive Events UDF

Overview

The AutoIt Drive Events UDF provides a set of functions to monitor and respond to drive-related events on a Windows system using the AutoIt scripting language. This UDF is particularly useful for detecting changes in drive labels, monitoring drive state (mounted/unmounted), and automating actions such as ejecting removable drives.

Functions

_CheckMountedLabels()

Checks for changes in the mounted labels of a specified drive and updates the drive information accordingly.

Parameters:
- None

Return values:
- None

__setter_Device_Control($hDevice, $dwIoControlCode, ByRef $iRead)

Performs device control operations on the specified device.

Parameters:
- $hDevice: A handle to the device on which the operation is to be performed.
- $dwIoControlCode: The control code for the operation.
- ByRef $iRead: [in/out] An integer variable to store the result of the operation.

Return values:
- True if the operation succeeds, False otherwise.

_driveEventOcurred()

Checks if a drive event has occurred.

Parameters:
- None

Return values:
- True if a drive event has occurred, False otherwise.

_GetDriveLabels([$sDriveName = "REMOVABLE"])

Gets the labels of drives of a specific type.

Parameters:
- $sDriveName: [optional] A string value. Default is "REMOVABLE".

Return values:
- Returns a string containing the labels of the specified drive type.

_RegisterDriveEvents($sDriveName = "REMOVABLE", $bReinitLabels = False, $bAutoEject = False)

Registers drive events for monitoring.

Parameters:
- $sDriveName: [optional] A string value representing the drive type. Default is "REMOVABLE".
- $bReinitLabels: [optional] A boolean value indicating whether to reinitialize labels. Default is False.
- $bAutoEject: [optional] A boolean value indicating whether to auto-eject the device on events. Default is False.

Return values:
- True on success, False on failure.

__WinAPI_AutoEjectVolume($hVolume)

Ejects the media from the volume.

Parameters:
- $hVolume: A handle value representing the volume.

Return values:
- True on success, False on failure.

Constants

Drive Information Constants

- $_g_sVOLUME_NAME: Index for volume name in drive event information.
- $_g_iSERIAL_NUMBER: Index for serial number in drive event information.
- $_g_iFILE_NAME_LENGTH: Index for file name length in drive event information.
- $_g_iFLAGS: Index for flags in drive event information.
- $_g_sFILE_SYSTEM: Index for file system in drive event information.
- $_g_sLABEL: Index for label in drive event information.
- $_g_sSTATE: Index for state in drive event information.
- $_g_DRIVE_TYPE: Index for drive type in drive event information.

Error Constants

- $ERROR_INVALID_PATH: Error code for invalid path.
- $ERROR_GET_VOLUME_INFO: Error code for failure to get volume information.
- $INVALID_HANDLE_VALUE: Constant representing an invalid handle value.

Examples

#include "DriveEvents.au3"
    ; Register events for removable drives and auto-eject on events
_RegisterDriveEvents("REMOVABLE", True, True)
    While 1
    If _driveEventOccurred() Then
        MsgBox(0, "Drive Event", "Drive " & _getEventDriveName() & " state: " & _getEventDriveState())
    EndIf
    Sleep(100)
WEnd

This example registers events for removable drives, reinitializes labels, and auto-ejects the device on events. The script continuously checks for drive events and displays a message box when an event occurs.


Example 1: Registering Drive Events and Handling Event Occurrence

#include "DriveEvents.au3"
    ; Register drive events for monitoring removable drives with auto-eject enabled
_RegisterDriveEvents("REMOVABLE", True, True)
    While 1
    If _driveEventOccurred() Then
        ConsoleWrite("Drive Event occurred!" & @CRLF)
        ConsoleWrite("Drive Name: " & _getEventDriveName() & @CRLF)
        ConsoleWrite("Volume Name: " & _getEventVolumeName() & @CRLF)
        ConsoleWrite("Drive State: " & _getEventDriveState() & @CRLF)
        ; Add more handling logic as needed
    EndIf
    Sleep(100)
WEnd

Example 2: Ejecting a Specific Drive

#include "DriveEvents.au3"
    ; Eject the media from drive "E:"
__WinAPI_EjectVolume("E:")

Example 3: Getting Volume Information

#include "DriveEvents.au3"
    ; Get volume information for the root directory of drive "C:"
Local $volumeInfo = __WinAPI_GetVolumeInformation("C:\")
    If Not @error Then
    ConsoleWrite("Volume Name: " & $volumeInfo[$_g_sVOLUME_NAME] & @CRLF)
    ConsoleWrite("Serial Number: " & $volumeInfo[$_g_iSERIAL_NUMBER] & @CRLF)
    ConsoleWrite("File System: " & $volumeInfo[$_g_sFILE_SYSTEM] & @CRLF)
    ; Add more information retrieval as needed
Else
    ConsoleWrite("Error getting volume information!" & @CRLF)
    ConsoleWrite("Error Code: " & @error & @CRLF)
EndIf

  

DriveEvents.au3

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...