problem with Drive, Lock

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

problem with Drive, Lock

Post by mauritius » 06 Nov 2020, 14:12

Hello,
First of all, welcome everybody on the new AHK forum, nice to meet you again!

I have a problem with locking my CD drive. I want to disable it so it won't open when I accidentally press the button.

I have a simple AHK script running always in the background. It has some simple keyboard fixes, nothing serious. It runs automatically on a system startup via a shortcut in:
C:\Users\Me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Now i want to add there an option to automatically lock my drive on a system startup.
I changed a tray menu and when I press it, it works like magic.
But it doesn't work when I add this line in the main code (not in a menu).
Can someone help me or point me in some direction?
Thanks in advance for your help.

Code: Select all

;
; AutoHotkey Version: 1.1.25.01
; Language:       English/Polish
; Platform:       Win7
; Author:         mauritius @ AHK forum
;
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
FileEncoding, UTF-8
#MaxMem 1024

Menu, Tray, NoStandard
Menu, Tray, Add, Pause Script, PauseScript
Menu, Tray, Add
Menu, Tray, Add, Lock CD, LockCD
Menu, Tray, Add, Unlock CD, UnLockCD
Menu, Tray, Add
Menu, Tray, Add, Exit

Sleep 5 * 60 * 1000 ;wait 5 minutes
Drive, Lock, F: ;it doesn't work here

Return

PauseScript:
Pause, Toggle
return

LockCD:
Drive, Lock, F: ;here it works
return

UnLockCD:
Drive, Unlock, F:
return

Exit:
ExitApp
return
User avatar
mikeyww
Posts: 27094
Joined: 09 Sep 2014, 18:38

Re: problem with Drive, Lock

Post by mikeyww » 06 Nov 2020, 14:31

It worked when I tried it. Of course, you have a 5-minute wait before the lock, right?

In some cases, optical drives are not immediately assigned to drive letters in Windows. It sometimes takes a minute or so. If your command executes before F: is assigned as a drive letter, the lock might fail. Of course, 5 minutes should be enough, but this also means that, with your 5-minute wait, the drive will be unlocked during that initial period.
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: problem with Drive, Lock

Post by mauritius » 06 Nov 2020, 19:06

mikeyww wrote:
06 Nov 2020, 14:31
It worked when I tried it. Of course, you have a 5-minute wait before the lock, right?

In some cases, optical drives are not immediately assigned to drive letters in Windows. It sometimes takes a minute or so. If your command executes before F: is assigned as a drive letter, the lock might fail. Of course, 5 minutes should be enough, but this also means that, with your 5-minute wait, the drive will be unlocked during that initial period.
Yes, I waited these 5 minutes. During my testing, I showed a traytip or a messagebox to be sure.

The problem is, sometimes it work, sometimes it doesn't.

I tried 5 minues. I tried 1, 2, 8.

Sometimes it works. And then, after like half an hour suddenly it doesn't. I have no idea why. This is why I can back here for help.
User avatar
mikeyww
Posts: 27094
Joined: 09 Sep 2014, 18:38

Re: problem with Drive, Lock

Post by mikeyww » 06 Nov 2020, 19:57

I think you are saying that it actually does lock, but then later becomes unlocked. Is that correct? Perhaps Windows or another process is unlocking the drive?

A way to test: run a timer that locks the drive every two seconds. See if the drive is still locked after 30 minutes. You can also check ErrorLevel after you lock.
If a drive is locked by a script and that script exits, the drive will stay locked until another script or program unlocks it, or the system is restarted. If the specified drive does not exist or does not support the locking feature, ErrorLevel is set to 1. Otherwise, it is set to 0.
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: problem with Drive, Lock

Post by mauritius » 07 Nov 2020, 18:39

mikeyww wrote:
06 Nov 2020, 19:57
I think you are saying that it actually does lock, but then later becomes unlocked. Is that correct? Perhaps Windows or another process is unlocking the drive?

A way to test: run a timer that locks the drive every two seconds. See if the drive is still locked after 30 minutes. You can also check ErrorLevel after you lock.
Yes, I tried it and the lock remains locked (after locking it every 5 minutes). I guess you're right that some other process unlocks it, but I have no idea what it may be.
ErrorLevel is 0 after locking a drive.

Oh, BTW thanks very much for your answers!

Two quick questions:
1. Is there any way to detect the current status (locked, unlocked) of my drive? I tried DriveGet, StatusCD but it seems no to be for this type of situation.
2. Is there a key/hotkey for ejecting button? I search in documentation and didn't find. I also used Autoscriptwriter to collect it but it didn't show anything.
User avatar
mikeyww
Posts: 27094
Joined: 09 Sep 2014, 18:38

Re: problem with Drive, Lock

Post by mikeyww » 07 Nov 2020, 19:53

Eject is easy.

Code: Select all

Drive, Eject
Doc states:
there is no way to determine whether a drive is currently locked
Nonetheless, Windows must know somehow, so there might be a workaround, a Windows call, etc., but I don't know what it would be.
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: problem with Drive, Lock

Post by mauritius » 07 Nov 2020, 23:23

I meant I wanted to change the behaviour of pressing the eject button - the same way as I can change the behaviour of , for example, Ctrl + W by using this hotkey

Code: Select all

^w
Anyway - thank you very much for your aswers.
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: problem with Drive, Lock

Post by mauritius » 10 Nov 2020, 00:41

Thanks for your links, they helped me learn but, even though I didn't solve the issue.
The problem is I can't identify the code of the eject button. I used @SKAN code from here but it doesn't show the key name when I press the eject button.

Thanks anyway for your help. I will use external software.

Thanks again for your answers!
User avatar
mikeyww
Posts: 27094
Joined: 09 Sep 2014, 18:38

Re: problem with Drive, Lock

Post by mikeyww » 10 Nov 2020, 06:05

The eject button is reportedly not registered as a key (from another recent post), though I suppose that this could vary by system.

What software works?
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: problem with Drive, Lock

Post by mauritius » 24 Nov 2020, 07:30

I use LockCD.

Sorry for delay in answering, I didn't get the notification that you answered.
User avatar
mikeyww
Posts: 27094
Joined: 09 Sep 2014, 18:38

Re: problem with Drive, Lock

Post by mikeyww » 24 Nov 2020, 17:04

OK. From reading the description, it appears that LockCD might be doing the same thing as Drive, Lock, and also using a loop. The description does not suggest that the eject button is remapped in any way, but I did not review the source code (and none appears to be provided).
Post Reply

Return to “Ask for Help (v1)”