-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclip98.cpp
155 lines (131 loc) · 3.73 KB
/
clip98.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
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
#include <string>
#include <vector>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
const char* wndClassName = "ClipboardExchanger";
HANDLE hComm;
HWND nextClipboardViewer = NULL;
bool justReadt = false;
void SetupSerialPort(HWND hwnd) {
hComm = CreateFileA("\\\\.\\COM1",
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE) {
MessageBoxEx(hwnd, "Error opening serial port", "Error", MB_OK, NULL);
exit(1);
}
DCB dcb = {0};
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
if (SetCommState(hComm, &dcb) == FALSE) {
MessageBoxEx(hwnd, "Error setting up serial port", "Error", MB_OK, NULL);
exit(1);
}
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
SetCommTimeouts(hComm, &timeouts);
}
void SendClipboard(HWND hwnd) {
OpenClipboard(hwnd);
HANDLE hData = GetClipboardData(CF_TEXT);
char* pszText = static_cast<char*>(GlobalLock(hData));
std::string text(pszText);
GlobalUnlock(hData);
CloseClipboard();
if (text.size() > 0 && !justReadt) {
unsigned long written;
WriteFile(hComm, text.c_str(), text.size(), &written, NULL);
WriteFile(hComm, "\f", 1, &written, NULL);
}
justReadt = false;
}
void RecieveData(HWND hwnd) {
char readData;
std::vector<char> chars;
unsigned long nBytesRead = 0;
do {
ReadFile(hComm, &readData, sizeof(readData), &nBytesRead, NULL);
if (nBytesRead > 0) {
chars.push_back(readData);
}
} while (nBytesRead > 0);
if (chars.size() > 0) {
chars.push_back(0);
unsigned long length = chars.size();
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, length);
char* ptr = (char*)GlobalLock(hMem);
std::copy(chars.begin(), chars.end(), ptr);
GlobalUnlock(hMem);
OpenClipboard(hwnd);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
justReadt = true;
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
nextClipboardViewer = SetClipboardViewer(hwnd);
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
CloseHandle(hComm);
ChangeClipboardChain(hwnd, nextClipboardViewer);
PostQuitMessage(0);
return 0;
case WM_CHANGECBCHAIN:
if((HWND) wParam == nextClipboardViewer){
nextClipboardViewer = (HWND) lParam;
} else if (nextClipboardViewer != NULL) {
SendMessage(nextClipboardViewer, msg, wParam, lParam);
}
return 0;
case WM_DRAWCLIPBOARD:
SendClipboard(hwnd);
if (nextClipboardViewer != NULL) {
SendMessage(nextClipboardViewer, msg, wParam, lParam);
}
return 0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszClassName = wndClassName;
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
wndClassName,
"ClipboardExchanger",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
SetupSerialPort(hwnd);
MSG msg = {0};
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
RecieveData(hwnd);
}
}
return msg.wParam;
}