You are here

autohotkey eject script (Basic Idea)

10 posts / 0 new
Last post
Nathan9222
Nathan9222's picture
Offline
Last seen: 3 years 1 day ago
Developer
Joined: 2007-12-06 22:35
autohotkey eject script (Basic Idea)

Alright, I know in the past I started to look into how to make an autohotkey script that would automatically eject the drive you have the PA menu on (when you click the eject button on PA menu). Well I began to write the script, and I made a basic one that works. Here it is.....

DriveGet, list,List, REMOVABLE  ;Only gets Removable Drives.
DriveGet, cap, capacity, %list%:  ;Gets total usb size
DrivespaceFree, free, %list%:  ;Tells you available space on drive
DriveGet, fs, fs, %list%:  ;Tells you what format the drive is in, eg: Fat32, Fat
DriveGet, label, label, %list%: ;Tells you what the usb Label name is, such as Nathan's Drive.
DriveGet, serial, serial, %list%: ;Tells you drives serial number
DriveGet, type, type, %list%:  ;Tells you what type of drive it is, Removable, Fixed
DriveGet, status, status, %list%: ;Tells you if the drive is able to be removed by telling you its ready

If list = 
ExitApp
else

MsgBox, 4, Eject, %list%:, Total Size:%cap%MB, Free Space:%free%MB, %fs%, %label%, %serial%, %type%, %status%, Is this information correct for your removable device? If so Click yes to Eject Drive %list%:
IfMsgBox No
ExitApp
else

Driveletter = %list%:
DrivePath = \\.\%Driveletter%

hVolume := DllCall("CreateFile"
      , str, DrivePath
      , UInt, 0x80000000 | 0x40000000   ;GENERIC_READ | GENERIC_WRITE
      , UInt, 0x0      ;Tries to get exclusiv rights to the drive +++(see below)
      , UInt, Null
      , UInt, 0x3         ;OPEN_EXISTING
      , UInt, 0x0
      , UInt, NULL)

if A_LastError = 32         ;Another application has read or write-access to the drive (In this case no handle was retreived to release)
{
   hVolume := DllCall("CreateFile"         ;Get handle even if another application reads the drive
      , str, DrivePath
      , UInt, 0x80000000 | 0x40000000   ;GENERIC_READ | GENERIC_WRITE
      , UInt, 0x1       ;FILE_SHARE_READ
      , UInt, Null
      , UInt, 0x3         ;OPEN_EXISTING
      , UInt, 0x0
      , UInt, NULL)
   
   if hVolume != -1
   {
      msgbox, 4164, Warning, An application is accessing this device.`nEject it anyway?
      IfMsgbox, No
      {
         DllCall("CloseHandle", UInt, hVolume)   ;Release handle here
         return
      }
   }
      
   if A_LastError = 32      ;No read access was possible also
   {
      msgbox, 4164, Warning!, Another application is writing to this device!`nEject it anyway?
      IfMsgbox, No
         return
      else
      {
         hVolume := DllCall("CreateFile"         ;Get handle even if another application reads or writes to the drive
            , str, DrivePath
            , UInt, 0x80000000 | 0x40000000   ;GENERIC_READ | GENERIC_WRITE
            , UInt, 0x1 | 0x2 ;FILE_SHARE_READ | FILE_SHARE_WRITE
            , UInt, Null
            , UInt, 0x3         ;OPEN_EXISTING
            , UInt, 0x0
            , UInt, NULL)
      }
   }
}

if hVolume != -1      ;Drive is Ejected
{
      DllCall("DeviceIoControl"
      , UInt, hVolume
      , UInt, 0x2D4808   ;IOCTL_STORAGE_EJECT_MEDIA
      , UInt, NULL
      , UInt, 0
      , UInt, NULL
      , UInt, 0
      , UInt, &dwBytesReturned   ;Not used
      , UInt,  NULL)

   DllCall("CloseHandle", UInt, hVolume)
}
return

Now I also have other variables that are listed that can be used to ensure the drive you are ejecting is yours and I could also add the ability to search the drive for the PA menu, and if it is present then it will continue to run the script, this is just an outline, and if you wish to add on to it then feel free to. I think this would be great for the PA menu's eject feature and hopefully people take some interest into this. Smile
You can get this script in exe form from here

José Pedro Arvela
Offline
Last seen: 5 years 9 months ago
Joined: 2007-07-10 07:29
AHK starter

I am an AHK starter (don't understand a thing about dll cals), but how about this:

  1. Detect the path of the eject script ( %A_SrciptDir% ),
  2. Extract the drive letter from the path (you do that),
  3. See if it is a removable drive (you do that too),
  4. Do all of those codes that I don't understand to remove it

That way, there is no need to ask the user if drive x is his drive.

Blue is everything.

Nathan9222
Nathan9222's picture
Offline
Last seen: 3 years 1 day ago
Developer
Joined: 2007-12-06 22:35
hmmm....

Just a thought, I know that there is a window that ask's you if that is you information, so I'm thinking maybe moving the message box code to the beginning and asks if that is correct then if you hit yes, then it will save to an .ini file in the %A_ScriptDir%, and put something like, GotDriveInfo=Yes, then exit the application and in the beginning of the application read GotDriveInfo=Yes, (If its not present then it wont run then next process, but take you to initial window where you will be asked if it is correct), then I can just include the "code" that will eject the drive in a seperate .ahk files. I think I know how I can do this a bit more complicated, but I think the eject script should be really simple so it doesnt get confusing. Smile
Also I need someone to clarify this.... is it true that usb serial numbers change from computer to computer? If no then I can always have the users serial read and this would probably have less flaws than another method.

An eye for an eye makes the whole world blind.
Mahatma Gandhi,
Indian political and spiritual leader (1869 - 1948)

digitxp
digitxp's picture
Offline
Last seen: 13 years 3 weeks ago
Joined: 2007-11-03 18:33
Hm...

I'm almost sure that all of that can be done in NSIS or (duh!) Delphi.

Insert original signature here with Greasemonkey Script.

ZachHudock
ZachHudock's picture
Offline
Last seen: 1 year 9 months ago
Developer
Joined: 2006-12-06 18:07
Hm...

I'm almost sure that you have been asked several times to drop the sarcastic tone. While all of it could be done in NSIS or (duh!) Delphi, AHK is Nathan's langauge of choice, it could also be done in C++, C, C#, JAVA, FreePascal, Assembly Language, and many others.

The developer formerly known as ZGitRDun8705

m2
Offline
Last seen: 13 years 7 months ago
Joined: 2006-12-12 12:00
Are you sure it can be done

Are you sure it can be done in JAVA? I barely know it, but I have some doubts..

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

ZachHudock
ZachHudock's picture
Offline
Last seen: 1 year 9 months ago
Developer
Joined: 2006-12-06 18:07
With the right

With the right frameworks/plugins JAVA can do just about anything. Whether or not using JAVA for the task is logical/efficient is a a different story...

The developer formerly known as ZGitRDun8705

digitxp
digitxp's picture
Offline
Last seen: 13 years 3 weeks ago
Joined: 2007-11-03 18:33
I was being sarcastic?

Gosh, guess lack of school really does turn brain matter to mush :(.
I was saying (duh!) Delphi because the menu was written in Delphi so the same algorithm for listing apps can be used to check close $drive\**.exe.
I'm not the sarcastic type (my friend the math wiz is :P).

Insert original signature here with Greasemonkey Script.

m2
Offline
Last seen: 13 years 7 months ago
Joined: 2006-12-12 12:00
I think you might find this

I think you might find this interesting.

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

KickButts
KickButts's picture
Offline
Last seen: 14 years 3 months ago
Joined: 2008-03-13 09:58
:)

I'll keep an eye on this thread since I'm very interested on the outcome.
Congrats on the work so far.

Alive and kicking!
"If you were a robot, and I knew but you didn't, would you want me to tell you?"

Log in or register to post comments