codekingpro/portable-devtools
114k
1import pythoncom2import win32con3 4formats = """CF_TEXT CF_BITMAP CF_METAFILEPICT CF_SYLK CF_DIF CF_TIFF5 CF_OEMTEXT CF_DIB CF_PALETTE CF_PENDATA CF_RIFF CF_WAVE6 CF_UNICODETEXT CF_ENHMETAFILE CF_HDROP CF_LOCALE CF_MAX7 CF_OWNERDISPLAY CF_DSPTEXT CF_DSPBITMAP CF_DSPMETAFILEPICT8 CF_DSPENHMETAFILE""".split()9format_name_map = {}10for f in formats:11 val = getattr(win32con, f)12 format_name_map[val] = f13 14tymeds = [attr for attr in pythoncom.__dict__.keys() if attr.startswith("TYMED_")]15 16 17def DumpClipboard():18 do = pythoncom.OleGetClipboard()19 print("Dumping all clipboard formats...")20 for fe in do.EnumFormatEtc():21 fmt, td, aspect, index, tymed = fe22 tymeds_this = [23 getattr(pythoncom, t) for t in tymeds if tymed & getattr(pythoncom, t)24 ]25 print("Clipboard format", format_name_map.get(fmt, str(fmt)))26 for t_this in tymeds_this:27 # As we are enumerating there should be no need to call28 # QueryGetData, but we do anyway!29 fetc_query = fmt, td, aspect, index, t_this30 try:31 do.QueryGetData(fetc_query)32 except pythoncom.com_error:33 print("Eeek - QGD indicated failure for tymed", t_this)34 # now actually get it.35 try:36 medium = do.GetData(fetc_query)37 except pythoncom.com_error as exc:38 print("Failed to get the clipboard data:", exc)39 continue40 if medium.tymed == pythoncom.TYMED_GDI:41 data = "GDI handle %d" % medium.data42 elif medium.tymed == pythoncom.TYMED_MFPICT:43 data = "METAFILE handle %d" % medium.data44 elif medium.tymed == pythoncom.TYMED_ENHMF:45 data = "ENHMETAFILE handle %d" % medium.data46 elif medium.tymed == pythoncom.TYMED_HGLOBAL:47 data = "%d bytes via HGLOBAL" % len(medium.data)48 elif medium.tymed == pythoncom.TYMED_FILE:49 data = "filename '%s'" % data50 elif medium.tymed == pythoncom.TYMED_ISTREAM:51 stream = medium.data52 stream.Seek(0, 0)53 bytes = 054 while 1:55 chunk = stream.Read(4096)56 if not chunk:57 break58 bytes += len(chunk)59 data = "%d bytes via IStream" % bytes60 elif medium.tymed == pythoncom.TYMED_ISTORAGE:61 data = "a IStorage"62 else:63 data = "*** unknown tymed!"64 print(" -> got", data)65 do = None66 67 68if __name__ == "__main__":69 DumpClipboard()70 if pythoncom._GetInterfaceCount() + pythoncom._GetGatewayCount():71 print(72 "XXX - Leaving with %d/%d COM objects alive"73 % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())74 )75 