cerapi.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # A demo of the Windows CE Remote API
  2. #
  3. # This connects to a CE device, and interacts with it.
  4. import wincerapi
  5. import win32event
  6. import win32api
  7. import win32con
  8. import os
  9. import sys
  10. import getopt
  11. def DumpPythonRegistry():
  12. try:
  13. h = wincerapi.CeRegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore\\%s\\PythonPath" % sys.winver)
  14. except win32api.error:
  15. print "The remote device does not appear to have Python installed"
  16. return 0
  17. path, typ = wincerapi.CeRegQueryValueEx(h, None)
  18. print "The remote PythonPath is '%s'" % (str(path), )
  19. h.Close()
  20. return 1
  21. def DumpRegistry(root, level=0):
  22. # A recursive dump of the remote registry to test most functions.
  23. h = wincerapi.CeRegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, None)
  24. level_prefix = " " * level
  25. index = 0
  26. # Enumerate values.
  27. while 1:
  28. try:
  29. name, data, typ = wincerapi.CeRegEnumValue(root, index)
  30. except win32api.error:
  31. break
  32. print "%s%s=%s" % (level_prefix, name, repr(str(data)))
  33. index = index+1
  34. # Now enumerate all keys.
  35. index=0
  36. while 1:
  37. try:
  38. name, klass = wincerapi.CeRegEnumKeyEx(root, index)
  39. except win32api.error:
  40. break
  41. print "%s%s\\" % (level_prefix, name)
  42. subkey = wincerapi.CeRegOpenKeyEx(root, name)
  43. DumpRegistry(subkey, level+1)
  44. index = index+1
  45. def DemoCopyFile():
  46. # Create a file on the device, and write a string.
  47. cefile = wincerapi.CeCreateFile("TestPython", win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None)
  48. wincerapi.CeWriteFile(cefile, "Hello from Python")
  49. cefile.Close()
  50. # reopen the file and check the data.
  51. cefile = wincerapi.CeCreateFile("TestPython", win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
  52. if wincerapi.CeReadFile(cefile, 100) != "Hello from Python":
  53. print "Couldnt read the data from the device!"
  54. cefile.Close()
  55. # Delete the test file
  56. wincerapi.CeDeleteFile("TestPython")
  57. print "Created, wrote to, read from and deleted a test file!"
  58. def DemoCreateProcess():
  59. try:
  60. hp, ht, pid, tid = wincerapi.CeCreateProcess("Windows\\Python.exe", "", None, None, 0, 0, None, "", None)
  61. # Not necessary, except to see if handle closing raises an exception
  62. # (if auto-closed, the error is suppressed)
  63. hp.Close()
  64. ht.Close()
  65. print "Python is running on the remote device!"
  66. except win32api.error, (hr, fn, msg):
  67. print "Couldnt execute remote process -", msg
  68. def DumpRemoteMachineStatus():
  69. ACLineStatus, BatteryFlag, BatteryLifePercent, BatteryLifeTime, BatteryFullLifeTime, BackupBatteryFlag, BackupBatteryLifePercent, BackupBatteryLifeTime, BackupBatteryLifeTime = \
  70. wincerapi.CeGetSystemPowerStatusEx()
  71. if ACLineStatus:
  72. power = "AC"
  73. else:
  74. power = "battery"
  75. if BatteryLifePercent==255:
  76. batPerc = "unknown"
  77. else:
  78. batPerc = BatteryLifePercent
  79. print "The batteries are at %s%%, and is currently being powered by %s" % (batPerc, power)
  80. memLoad, totalPhys, availPhys, totalPage, availPage, totalVirt, availVirt = \
  81. wincerapi.CeGlobalMemoryStatus()
  82. print "The memory is %d%% utilized." % (memLoad)
  83. print "%-20s%-10s%-10s" % ("", "Total", "Avail")
  84. print "%-20s%-10s%-10s" % ("Physical Memory", totalPhys, availPhys)
  85. print "%-20s%-10s%-10s" % ("Virtual Memory", totalVirt, availVirt)
  86. print "%-20s%-10s%-10s" % ("Paging file", totalPage, availPage)
  87. storeSize, freeSize = wincerapi.CeGetStoreInformation()
  88. print "%-20s%-10s%-10s" % ("File store", storeSize, freeSize)
  89. print "The CE temp path is", wincerapi.CeGetTempPath()
  90. print "The system info for the device is", wincerapi.CeGetSystemInfo()
  91. def DumpRemoteFolders():
  92. # Dump all special folders possible.
  93. for name, val in wincerapi.__dict__.items():
  94. if name[:6]=="CSIDL_":
  95. try:
  96. loc = str(wincerapi.CeGetSpecialFolderPath(val))
  97. print "Folder %s is at %s" % (name, loc)
  98. except win32api.error, details:
  99. pass
  100. # Get the shortcut targets for the "Start Menu"
  101. print "Dumping start menu shortcuts..."
  102. try:
  103. startMenu = str(wincerapi.CeGetSpecialFolderPath(wincerapi.CSIDL_STARTMENU))
  104. except win32api.error, details:
  105. print "This device has no start menu!", details
  106. startMenu = None
  107. if startMenu:
  108. for fileAttr in wincerapi.CeFindFiles(os.path.join(startMenu, "*")):
  109. fileName = fileAttr[8]
  110. fullPath = os.path.join(startMenu, str(fileName))
  111. try:
  112. resolved = wincerapi.CeSHGetShortcutTarget(fullPath)
  113. except win32api.error, (rc, fn, msg):
  114. resolved = "#Error - %s" % msg
  115. print "%s->%s" % (fileName, resolved)
  116. # print "The start menu is at",
  117. # print wincerapi.CeSHGetShortcutTarget("\\Windows\\Start Menu\\Shortcut to Python.exe.lnk")
  118. def usage():
  119. print "Options:"
  120. print "-a - Execute all demos"
  121. print "-p - Execute Python process on remote device"
  122. print "-r - Dump the remote registry"
  123. print "-f - Dump all remote special folder locations"
  124. print "-s - Dont dump machine status"
  125. print "-y - Perform asynch init of CE connection"
  126. def main():
  127. async_init = bStartPython = bDumpRegistry = bDumpFolders = 0
  128. bDumpStatus = 1
  129. try:
  130. opts, args = getopt.getopt(sys.argv[1:], "apr")
  131. except getopt.error, why:
  132. print "Invalid usage:", why
  133. usage()
  134. return
  135. for o, v in opts:
  136. if o=="-a":
  137. bStartPython = bDumpRegistry = bDumpStatus = bDumpFolders = asynch_init = 1
  138. if o=="-p":
  139. bStartPython=1
  140. if o=="-r":
  141. bDumpRegistry=1
  142. if o=="-s":
  143. bDumpStatus=0
  144. if o=="-f":
  145. bDumpFolders = 1
  146. if o=="-y":
  147. print "Doing asynch init of CE connection"
  148. async_init = 1
  149. if async_init:
  150. event, rc = wincerapi.CeRapiInitEx()
  151. while 1:
  152. rc = win32event.WaitForSingleObject(event, 500)
  153. if rc==win32event.WAIT_OBJECT_0:
  154. # We connected.
  155. break
  156. else:
  157. print "Waiting for Initialize to complete (picture a Cancel button here :)"
  158. else:
  159. wincerapi.CeRapiInit()
  160. print "Connected to remote CE device."
  161. try:
  162. verinfo = wincerapi.CeGetVersionEx()
  163. print "The device is running windows CE version %d.%d - %s" % (verinfo[0], verinfo[1], verinfo[4])
  164. if bDumpStatus:
  165. print "Dumping remote machine status"
  166. DumpRemoteMachineStatus()
  167. if bDumpRegistry:
  168. print "Dumping remote registry..."
  169. DumpRegistry(win32con.HKEY_LOCAL_MACHINE)
  170. if bDumpFolders:
  171. print "Dumping remote folder information"
  172. DumpRemoteFolders()
  173. DemoCopyFile()
  174. if bStartPython:
  175. print "Starting remote Python process"
  176. if DumpPythonRegistry():
  177. DemoCreateProcess()
  178. else:
  179. print "Not trying to start Python, as it's not installed"
  180. finally:
  181. wincerapi.CeRapiUninit()
  182. print "Disconnected"
  183. if __name__=='__main__':
  184. main()