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.

dump_clipboard.py 2.9KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import pythoncom
  2. import win32con
  3. formats = """CF_TEXT CF_BITMAP CF_METAFILEPICT CF_SYLK CF_DIF CF_TIFF
  4. CF_OEMTEXT CF_DIB CF_PALETTE CF_PENDATA CF_RIFF CF_WAVE
  5. CF_UNICODETEXT CF_ENHMETAFILE CF_HDROP CF_LOCALE CF_MAX
  6. CF_OWNERDISPLAY CF_DSPTEXT CF_DSPBITMAP CF_DSPMETAFILEPICT
  7. CF_DSPENHMETAFILE""".split()
  8. format_name_map = {}
  9. for f in formats:
  10. val = getattr(win32con, f)
  11. format_name_map[val] = f
  12. tymeds = [attr for attr in pythoncom.__dict__.keys() if attr.startswith("TYMED_")]
  13. def DumpClipboard():
  14. do = pythoncom.OleGetClipboard()
  15. print("Dumping all clipboard formats...")
  16. for fe in do.EnumFormatEtc():
  17. fmt, td, aspect, index, tymed = fe
  18. tymeds_this = [
  19. getattr(pythoncom, t) for t in tymeds if tymed & getattr(pythoncom, t)
  20. ]
  21. print("Clipboard format", format_name_map.get(fmt, str(fmt)))
  22. for t_this in tymeds_this:
  23. # As we are enumerating there should be no need to call
  24. # QueryGetData, but we do anyway!
  25. fetc_query = fmt, td, aspect, index, t_this
  26. try:
  27. do.QueryGetData(fetc_query)
  28. except pythoncom.com_error:
  29. print("Eeek - QGD indicated failure for tymed", t_this)
  30. # now actually get it.
  31. try:
  32. medium = do.GetData(fetc_query)
  33. except pythoncom.com_error as exc:
  34. print("Failed to get the clipboard data:", exc)
  35. continue
  36. if medium.tymed == pythoncom.TYMED_GDI:
  37. data = "GDI handle %d" % medium.data
  38. elif medium.tymed == pythoncom.TYMED_MFPICT:
  39. data = "METAFILE handle %d" % medium.data
  40. elif medium.tymed == pythoncom.TYMED_ENHMF:
  41. data = "ENHMETAFILE handle %d" % medium.data
  42. elif medium.tymed == pythoncom.TYMED_HGLOBAL:
  43. data = "%d bytes via HGLOBAL" % len(medium.data)
  44. elif medium.tymed == pythoncom.TYMED_FILE:
  45. data = "filename '%s'" % data
  46. elif medium.tymed == pythoncom.TYMED_ISTREAM:
  47. stream = medium.data
  48. stream.Seek(0, 0)
  49. bytes = 0
  50. while 1:
  51. chunk = stream.Read(4096)
  52. if not chunk:
  53. break
  54. bytes += len(chunk)
  55. data = "%d bytes via IStream" % bytes
  56. elif medium.tymed == pythoncom.TYMED_ISTORAGE:
  57. data = "a IStorage"
  58. else:
  59. data = "*** unknown tymed!"
  60. print(" -> got", data)
  61. do = None
  62. if __name__ == "__main__":
  63. DumpClipboard()
  64. if pythoncom._GetInterfaceCount() + pythoncom._GetGatewayCount():
  65. print(
  66. "XXX - Leaving with %d/%d COM objects alive"
  67. % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  68. )