-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.c
308 lines (261 loc) · 6.36 KB
/
clipboard.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Kernel32.lib")
#include <windows.h>
#include "clipboard.h"
#include <stdbool.h>
#include <strsafe.h>
void display_last_error();
void GetClipboardFormatName_helper (UINT uFormat, char *formats, char szFormatName[80], LPCSTR lpFormatName);
LIB_EXPORT int count_formats() {
int a = CountClipboardFormats();
return a;
}
LIB_EXPORT char *list_available_format() {
UINT uFormat;
LPCSTR formats = TEXT("");
LPCSTR lpFormatName = NULL;
char szFormatName[80];
int i = 0;
// Open the clipboard.
if (!OpenClipboard(NULL))
return NULL;
int a = CountClipboardFormats();
if(a == 0)
{
CloseClipboard();
return NULL;
}
uFormat = EnumClipboardFormats(0);
while (uFormat)
{
GetClipboardFormatName_helper(uFormat, formats, szFormatName, lpFormatName);
if(i < a - 1)
formats = lstrcat(formats, TEXT(","));
i++;
uFormat = EnumClipboardFormats(uFormat);
}
CloseClipboard();
return formats;
}
LIB_EXPORT char *get_text() {
if(IsClipboardFormatAvailable(CF_TEXT))
{
OpenClipboard(NULL);
HGLOBAL hglb = GetClipboardData(CF_TEXT);
CloseClipboard();
return hglb;
}
return NULL;
}
LIB_EXPORT unsigned char *get_image(size_t *loaded) {
UINT png_format = RegisterClipboardFormatA("PNG");
if (png_format && IsClipboardFormatAvailable(png_format)) {
printf("clipboard.c >> find png\n");
OpenClipboard(NULL);
HANDLE png_handle = GetClipboardData(png_format);
size_t png_size = GlobalSize(png_handle);
*loaded = png_size;
CloseClipboard();
return png_handle;
}else if(IsClipboardFormatAvailable(CF_BITMAP)){
printf("clipboard.c >> find Bitmap\n");
OpenClipboard(NULL);
HGLOBAL png_handle = GetClipboardData(CF_BITMAP);
size_t png_size = GlobalSize(png_handle);
*loaded = png_size;
CloseClipboard();
return png_handle;
}else if(IsClipboardFormatAvailable(CF_DIB)){
printf("clipboard.c >> find DIB\n");
OpenClipboard(NULL);
HGLOBAL png_handle = GetClipboardData(CF_DIB);
size_t png_size = GlobalSize(png_handle);
*loaded = png_size;
CloseClipboard();
return png_handle;
}
return NULL;
}
LIB_EXPORT char *get_data(const char *type) {
int cfid = RegisterClipboardFormat(type);
if(IsClipboardFormatAvailable(cfid))
{
OpenClipboard(NULL);
HGLOBAL hglb = GetClipboardData(cfid);
CloseClipboard();
return hglb;
}
return NULL;
}
LIB_EXPORT unsigned char *get_bytes(const char *type, size_t *loaded) {
int cfid = RegisterClipboardFormat(type);
if(IsClipboardFormatAvailable(cfid))
{
OpenClipboard(NULL);
HGLOBAL hglb = GetClipboardData(cfid);
size_t size = GlobalSize(hglb);
*loaded = size;
CloseClipboard();
return hglb;
}
return NULL;
}
LIB_EXPORT bool clear() {
if (!OpenClipboard(NULL))
return FALSE;
bool isSuccess = EmptyClipboard();
CloseClipboard();
return isSuccess;
}
LIB_EXPORT bool set_text(const char *str) {
//GetForegroundWindow()
if (!OpenClipboard(0))
{
display_last_error();
return FALSE;
}
const size_t len = strlen(str) + 1;
HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hglb), str, len);
GlobalUnlock(hglb);
//EmptyClipboard();
HANDLE res = SetClipboardData(CF_TEXT, hglb);
bool isSet = res ? TRUE: FALSE;
if(!isSet)
{
GlobalFree(hglb);
display_last_error();
}
CloseClipboard();
return isSet;
}
LIB_EXPORT bool set_data(const char *type, const char *str) {
if (!OpenClipboard(0))
{
display_last_error();
return FALSE;
}
int cfid = RegisterClipboardFormat(type);
const size_t len = strlen(str) + 1;
HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hglb), str, len);
GlobalUnlock(hglb);
//EmptyClipboard();
HANDLE res = SetClipboardData(cfid, hglb);
bool isSet = res ? TRUE: FALSE;
if(!isSet)
{
GlobalFree(hglb);
display_last_error();
}
CloseClipboard();
return isSet;
}
LIB_EXPORT bool set_image(unsigned char *data, int len)
{
if (!OpenClipboard(0))
{
display_last_error();
return FALSE;
}
int cfid = RegisterClipboardFormat("PNG");
HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hglb), data, len);
GlobalUnlock(hglb);
//EmptyClipboard();
HANDLE res = SetClipboardData(cfid, hglb);
bool isSet = res ? TRUE: FALSE;
if(!isSet)
{
GlobalFree(hglb);
display_last_error();
}
CloseClipboard();
return isSet;
}
LIB_EXPORT bool set_bytes(const char *type, unsigned char *data, int len)
{
if (!OpenClipboard(0))
{
display_last_error();
return FALSE;
}
int cfid = RegisterClipboardFormat(type);
HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hglb), data, len);
GlobalUnlock(hglb);
//EmptyClipboard();
HANDLE res = SetClipboardData(cfid, hglb);
bool isSet = res ? TRUE: FALSE;
if(!isSet)
{
GlobalFree(hglb);
display_last_error();
}
CloseClipboard();
return isSet;
}
void GetClipboardFormatName_helper (UINT uFormat, char *formats, char szFormatName[80], LPCSTR lpFormatName)
{
switch (uFormat)
{
case CF_TEXT:
lpFormatName = TEXT("Text");
break;
case CF_UNICODETEXT:
lpFormatName = TEXT("Unicode Text Format");
break;
case CF_OEMTEXT:
lpFormatName = TEXT("OEM Text");
break;
case CF_LOCALE:
lpFormatName = TEXT("Locale Identifier");
break;
case CF_BITMAP:
lpFormatName = TEXT("Bitmap");
break;
case CF_DIB:
lpFormatName = TEXT("CF_DIB");
break;
case CF_TIFF:
lpFormatName = TEXT("CF_TIFF");
break;
default:
if(GetClipboardFormatName(uFormat, szFormatName, 80))
{
lpFormatName = szFormatName;
}else{
lpFormatName = TEXT("unknown");
}
break;
}
formats = lstrcat(formats, lpFormatName);
}
void display_last_error()
{
DWORD dw = GetLastError();
if(!dw)
return;
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("clipboard.c >> Error %d: %s"), dw, lpMsgBuf);
printf(lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}