MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / DriveLock

Method DriveLock

Source/script2.cpp:8024–8109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8022
8023
8024ResultType Line::DriveLock(char aDriveLetter, bool aLockIt)
8025{
8026 HANDLE hdevice;
8027 DWORD unused;
8028 BOOL result;
8029
8030 if (g_os.IsWin9x())
8031 {
8032 // blisteringhot@hotmail.com has confirmed that the code below works on Win98 with an IDE CD Drive:
8033 // System: Win98 IDE CdRom (my ejecter is CloseTray)
8034 // I get a blue screen when I try to eject after using the test script.
8035 // "eject request to drive in use"
8036 // It asks me to Ok or Esc, Ok is default.
8037 // -probably a bit scary for a novice.
8038 // BUT its locked alright!"
8039
8040 // Use the Windows 9x method. The code below is based on an example posted by Microsoft.
8041 // Note: The presence of the code below does not add a detectible amount to the EXE size
8042 // (probably because it's mostly defines and data types).
8043 #pragma pack(1)
8044 typedef struct _DIOC_REGISTERS
8045 {
8046 DWORD reg_EBX;
8047 DWORD reg_EDX;
8048 DWORD reg_ECX;
8049 DWORD reg_EAX;
8050 DWORD reg_EDI;
8051 DWORD reg_ESI;
8052 DWORD reg_Flags;
8053 } DIOC_REGISTERS, *PDIOC_REGISTERS;
8054 typedef struct _PARAMBLOCK
8055 {
8056 BYTE Operation;
8057 BYTE NumLocks;
8058 } PARAMBLOCK, *PPARAMBLOCK;
8059 #pragma pack()
8060
8061 // MS: Prepare for lock or unlock IOCTL call
8062 #define CARRY_FLAG 0x1
8063 #define VWIN32_DIOC_DOS_IOCTL 1
8064 #define LOCK_MEDIA 0
8065 #define UNLOCK_MEDIA 1
8066 #define STATUS_LOCK 2
8067 PARAMBLOCK pb = {0};
8068 pb.Operation = aLockIt ? LOCK_MEDIA : UNLOCK_MEDIA;
8069
8070 DIOC_REGISTERS regs = {0};
8071 regs.reg_EAX = 0x440D;
8072 regs.reg_EBX = toupper(aDriveLetter) - 'A' + 1; // Convert to drive index. 0 = default, 1 = A, 2 = B, 3 = C
8073 regs.reg_ECX = 0x0848; // MS: Lock/unlock media
8074 regs.reg_EDX = (DWORD)&pb;
8075
8076 // MS: Open VWIN32
8077 hdevice = CreateFile("\\\\.\\vwin32", 0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);
8078 if (hdevice == INVALID_HANDLE_VALUE)
8079 return FAIL;
8080
8081 // MS: Call VWIN32

Callers

nothing calls this directly

Calls 1

IsWin9xMethod · 0.80

Tested by

no test coverage detected