|
10 | 10 | if platform != 'win':
|
11 | 11 | raise SystemError('unsupported platform for Windows clipboard')
|
12 | 12 |
|
| 13 | +import ctypes |
13 | 14 | user32 = ctypes.windll.user32
|
14 | 15 | kernel32 = ctypes.windll.kernel32
|
15 | 16 | msvcrt = ctypes.cdll.msvcrt
|
16 | 17 | c_char_p = ctypes.c_char_p
|
| 18 | +c_wchar_p = ctypes.c_wchar_p |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class ClipboardWindows(ClipboardBase):
|
20 | 22 |
|
21 | 23 | def get(self, mimetype='text/plain'):
|
22 | 24 | user32.OpenClipboard(0)
|
23 | 25 | # 1 is CF_TEXT
|
24 |
| - pcontents = user32.GetClipboardData(1) |
25 |
| - data = c_char_p(pcontents).value |
| 26 | + pcontents = user32.GetClipboardData(13) |
| 27 | + data = c_wchar_p(pcontents).value.encode('utf-16') |
26 | 28 | #ctypes.windll.kernel32.GlobalUnlock(pcontents)
|
27 | 29 | user32.CloseClipboard()
|
28 | 30 | return data
|
29 | 31 |
|
30 |
| - def put(self, data, mimetype='text/plain'): |
| 32 | + def put(self, text, mimetype='text/plain'): |
| 33 | + print text, type(text) |
31 | 34 | GMEM_DDESHARE = 0x2000
|
32 |
| - user32.OpenClipboard(0) |
| 35 | + CF_UNICODETEXT = 13 |
| 36 | + user32.OpenClipboard(None) |
33 | 37 | user32.EmptyClipboard()
|
34 |
| - try: |
35 |
| - # works on Python 2 (bytes() only takes one argument) |
36 |
| - hCd = kernel32.GlobalAlloc( |
37 |
| - GMEM_DDESHARE, len(bytes(text)) + 1) |
38 |
| - except TypeError: |
39 |
| - # works on Python 3 (bytes() requires an encoding) |
40 |
| - hCd = kernel32.GlobalAlloc( |
41 |
| - GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1) |
| 38 | + hCd = kernel32.GlobalAlloc(GMEM_DDESHARE, len(text) + 2) |
42 | 39 | pchData = kernel32.GlobalLock(hCd)
|
43 |
| - try: |
44 |
| - # works on Python 2 (bytes() only takes one argument) |
45 |
| - msvcrt.strcpy( |
46 |
| - c_char_p(pchData), bytes(text)) |
47 |
| - except TypeError: |
48 |
| - # works on Python 3 (bytes() requires an encoding) |
49 |
| - msvcrt.strcpy( |
50 |
| - c_char_p(pchData), bytes(text, 'ascii')) |
| 40 | + msvcrt.wcscpy(c_wchar_p(pchData), text) |
51 | 41 | kernel32.GlobalUnlock(hCd)
|
52 |
| - user32.SetClipboardData(1, hCd) |
| 42 | + user32.SetClipboardData(CF_UNICODETEXT, hCd) |
53 | 43 | user32.CloseClipboard()
|
54 | 44 |
|
55 | 45 | def get_types(self):
|
56 | 46 | return list('text/plain',)
|
57 | 47 |
|
| 48 | + |
0 commit comments