Skip to content

Instantly share code, notes, and snippets.

@globby
Created March 8, 2014 02:59
Show Gist options
  • Save globby/9424649 to your computer and use it in GitHub Desktop.
Save globby/9424649 to your computer and use it in GitHub Desktop.
A simple clipboard modification class with ctypes
from ctypes import *
class Clipboard:
def __init__(self):
self.strcpy = cdll.msvcrt.strcpy
self.OpenClipboard = windll.user32.OpenClipboard
self.CloseClipboard = windll.user32.CloseClipboard
self.EmptyClipboard = windll.user32.EmptyClipboard
self.GetClipboardData = windll.user32.GetClipboardData
self.SetClipboardData = windll.user32.SetClipboardData
self.GlobalAlloc = windll.kernel32.GlobalAlloc
self.GlobalLock = windll.kernel32.GlobalLock
self.GlobalUnlock = windll.kernel32.GlobalUnlock
self.GMEM_DDESHARE = 0x2000
def getClipboard(self):
self.OpenClipboard(c_int(0))
contents = c_char_p(self.GetClipboardData(c_int(1))).value
self.CloseClipboard()
return contents
def emptyClipboard(self):
self.OpenClipboard(c_int(0))
self.EmptyClipboard()
self.CloseClipboard()
def setClipboard(self,data):
self.OpenClipboard(c_int(0))
self.EmptyClipboard()
alloc = self.GlobalAlloc(self.GMEM_DDESHARE, len(bytes(data))+1)
lock = self.GlobalLock(alloc)
self.strcpy(c_char_p(lock),bytes(data))
self.GlobalUnlock(alloc)
self.SetClipboardData(c_int(1),alloc)
self.CloseClipboard()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment