Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cerapi.py 7.7KB

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