1__author__ = "Israel Fruchter <israel.fruchter@gmail.com>" 2 3import sys, os 4 5if not sys.platform == 'win32': 6 raise Exception("Windows-only demo") 7 8try: 9 from _winclipboard_cffi import ffi, lib 10except ImportError: 11 print 'run winclipboard_build first, then make sure the shared object is on sys.path' 12 sys.exit(1) 13 14# ffi "knows" about the declared variables and functions from the 15# cdef parts of the module _winclipboard_cffi created, 16# lib "knows" how to call the functions from the set_source parts 17# of the module. 18 19def CopyToClipboard(string): 20 ''' 21 use win32 api to copy `string` to the clipboard 22 ''' 23 hWnd = lib.GetConsoleWindow() 24 25 if lib.OpenClipboard(hWnd): 26 cstring = ffi.new("char[]", string) 27 size = ffi.sizeof(cstring) 28 29 # make it a moveable memory for other processes 30 hGlobal = lib.GlobalAlloc(lib.GMEM_MOVEABLE, size) 31 buffer = lib.GlobalLock(hGlobal) 32 lib.memcpy(buffer, cstring, size) 33 lib.GlobalUnlock(hGlobal) 34 35 res = lib.EmptyClipboard() 36 res = lib.SetClipboardData(lib.CF_TEXT, buffer) 37 38 lib.CloseClipboard() 39 40CopyToClipboard("hello world from cffi") 41