MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / DriveLock

Function DriveLock

source/script2.cpp:6879–6902  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6877
6878
6879ResultType DriveLock(TCHAR aDriveLetter, bool aLockIt)
6880{
6881 HANDLE hdevice;
6882 DWORD unused;
6883 BOOL result;
6884 TCHAR filename[64];
6885 _stprintf(filename, _T("\\\\.\\%c:"), aDriveLetter);
6886 // FILE_READ_ATTRIBUTES is not enough; it yields "Access Denied" error. So apparently all or part
6887 // of the sub-attributes in GENERIC_READ are needed. An MSDN example implies that GENERIC_WRITE is
6888 // only needed for GetDriveType() == DRIVE_REMOVABLE drives, and maybe not even those when all we
6889 // want to do is lock/unlock the drive (that example did quite a bit more). In any case, research
6890 // indicates that all CD/DVD drives are ever considered DRIVE_CDROM, not DRIVE_REMOVABLE.
6891 // Due to this and the unlikelihood that GENERIC_WRITE is ever needed anyway, GetDriveType() is
6892 // not called for the purpose of conditionally adding the GENERIC_WRITE attribute.
6893 hdevice = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
6894 if (hdevice == INVALID_HANDLE_VALUE)
6895 return FAIL;
6896 PREVENT_MEDIA_REMOVAL pmr;
6897 pmr.PreventMediaRemoval = aLockIt;
6898 result = DeviceIoControl(hdevice, IOCTL_STORAGE_MEDIA_REMOVAL, &pmr, sizeof(PREVENT_MEDIA_REMOVAL)
6899 , NULL, 0, &unused, NULL);
6900 CloseHandle(hdevice);
6901 return result ? OK : FAIL;
6902}
6903
6904
6905

Callers 1

BIF_DECLFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected