-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.cpp
55 lines (51 loc) · 1.6 KB
/
clipboard.cpp
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
#include "clipboard.h"
#include "window.h"
#include "log.h"
void Clipboard::CopyToClipboard(const std::string& text) {
HGLOBAL movable_mem {};
LPSTR buffer = NULL;
if (!Window::IsInit()) {
Log::print(L"Impossibile copiare nella clipboard: la finestra di soli messaggi non è inizializzata");
return;
}
if (OpenClipboard(Window::Hwnd()) == 0) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile aprire la clipboard", error_code);
return;
}
if (EmptyClipboard() == 0) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile azzerare la clipboard", error_code);
CloseClipboard();
return;
}
movable_mem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, text.size() + 1);
if (movable_mem == NULL) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile allocare memoria con GlobalAlloc", error_code);
CloseClipboard();
return;
}
buffer = (LPSTR) GlobalLock(movable_mem);
if (buffer == NULL) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile aprire memoria con GlobalLock", error_code);
GlobalFree(movable_mem);
CloseClipboard();
return;
}
memcpy(buffer, text.c_str(), text.size());
buffer[text.size()] = 0;
GlobalUnlock(movable_mem);
if (SetClipboardData(CF_TEXT, (HANDLE)movable_mem) == NULL) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile impostare la clipboard", error_code);
GlobalFree(movable_mem);
CloseClipboard();
return;
}
if (CloseClipboard() == 0) {
DWORD error_code = GetLastError();
Log::print_error_code(L"Impossibile chiudere la clipboard", error_code);
}
}