-
Notifications
You must be signed in to change notification settings - Fork 3
/
clipboard_windows.c
76 lines (60 loc) · 1.13 KB
/
clipboard_windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <windows.h>
char *get() {
char *ret = NULL;
int sz;
HANDLE h;
if (!OpenClipboard(NULL))
goto done;
h = GetClipboardData(CF_UNICODETEXT);
if (!h)
goto close;
if (!GlobalLock(h))
goto close;
sz = WideCharToMultiByte(CP_UTF8, 0, h, -1, NULL, 0, NULL, NULL);
if (!sz)
goto unlock;
ret = malloc(sz);
if (!ret)
goto unlock;
sz = WideCharToMultiByte(CP_UTF8, 0, h, -1, ret, sz, NULL, NULL);
if (!sz) {
free(ret);
ret = NULL;
}
unlock:
GlobalUnlock(h);
close:
CloseClipboard();
done:
return ret;
}
int set(const char *s) {
int ret = 0;
wchar_t *h;
int sz;
sz = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
if (!sz)
goto done;
h = GlobalAlloc(0, 2*sz);
if (!h)
goto done;
sz = MultiByteToWideChar(CP_UTF8, 0, s, -1, h, sz);
if (!sz) {
goto dealloc;
}
if (!OpenClipboard(NULL))
goto dealloc;
if (!EmptyClipboard())
goto dealloc;
if (SetClipboardData(CF_UNICODETEXT, h)) {
ret = 1;
goto close;
}
dealloc:
GlobalFree(h);
close:
CloseClipboard();
done:
return ret;
}