Welcome, guest | Sign In | My Account | Store | Cart

Of course, ActivePython is a great stuff and there are not problems which it can not solve. But I do like experiment with different technologies. The following script demonstrates how you can eject\close CDRom door with IronPython using standard CPython libraries.

Text, 44 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#standard python library location
import sys
sys.path.append(r"C:\Python27\Lib")

#import types
from ctypes import byref, wintypes, windll
from string import lower
from sys    import argv

#accessors
GENERIC_READ              = wintypes.DWORD(0x80000000)
IOCTL_STORAGE_EJECT_MEDIA = wintypes.DWORD(0x002D4808)
IOCTL_STORAGE_LOAD_MEDIA  = wintypes.DWORD(0x002D480C)
OPEN_EXISTING             = wintypes.DWORD(0x00000003)
#(possible) returned parameters
INVALID_HANDLE            = -1
retBytes                  = wintypes.DWORD()

#eject\close cdrom function
def EjectClose(drv, cur):
   dll = windll.kernel32 #accessor kernel32 library

   try:
      hndl = dll.CreateFileW(drv, GENERIC_READ, 0, None, OPEN_EXISTING, 0, None)

      if hndl != INVALID_HANDLE:
         if cur: #current status is true
            dll.DeviceIoControl(hndl, IOCTL_STORAGE_EJECT_MEDIA, None, 0, None, 0, byref(retBytes), None)
         else:
            dll.DeviceIoControl(hndl, IOCTL_STORAGE_LOAD_MEDIA, None, 0, None, 0, byref(retBytes), None)
   finally:
      dll.CloseHandle(hndl) #do not forget about this!
      hndl = None

if __name__ == '__main__':
   if len(argv) == 2:
      if lower(argv[1]) == "-c" or lower(argv[1]) == "/c":
         EjectClose(r"\\.\E:", False) #before launch scipt check cdrom's letter!
      elif lower(argv[1]) == "-e" or lower(argv[1]) == "/e":
         EjectClose(r"\\.\E:", True)
      else:
         print("Unknown %s parameter." % argv[1])
   else:
      print("Arguments are out of length.")