-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSync.c
executable file
·175 lines (149 loc) · 3.8 KB
/
TestSync.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
#include "TestSync.h"
#include "Sync.h"
HINSTANCE hInst;
WCHAR szTitle[] = L"TestSync";
WCHAR szMenuName[] = L"TestSync";
WCHAR szWindowClass[] = L"TestSync";
// TODO: Error handling with line numbers.
int APIENTRY
wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow
)
{
WNDCLASSEXW wcex;
HWND hWnd;
HACCEL hAccelTable;
MSG msg;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Add custom icon or use default Visual Studio icon.
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = szMenuName;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassExW(&wcex)) {
ErrorExit(L"RegisterClassExW");
}
hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd) {
ErrorExit(L"CreateWindowExW");
}
if (!SetupConsole()) {
ErrorExit(L"SetupConsole");
}
if (!SetupInput()) {
ErrorExit(L"SetupInput");
}
ShowWindow(hWnd, nCmdShow);
if (!UpdateWindow(hWnd)) {
ErrorExit(L"UpdateWindow");
}
HMODULE sync;
sync = LoadLibraryW(L".\\Sync.dll");
//printf("%d\n", Test());
hAccelTable = LoadAccelerators(hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(hWnd, NULL, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
LRESULT CALLBACK
WndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
UINT dwSize;
LPBYTE lpData;
RAWINPUT *ri;
switch (message) {
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
case WM_KEYDOWN: {
wprintf(L".\n");
break;
}
default: {
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
BOOL
SetupInput()
{
RAWINPUTDEVICE rid[1];
// Keyboard: ignore default window messages.
rid[0].usUsagePage = 0x01;
rid[0].usUsage = 0x06;
rid[0].dwFlags = RIDEV_NOLEGACY;
rid[0].hwndTarget = 0;
if (!RegisterRawInputDevices(rid, 1, sizeof(rid[0]))) {
return FALSE;
}
}
BOOL
SetupConsole()
{
FILE *fConsole;
if (!AllocConsole()) {
return FALSE;
}
freopen_s(&fConsole, "CONOUT$", "w", stdout);
freopen_s(&fConsole, "CONIN$", "r", stdin);
//freopen_s(&fConsole, "CONERR$", "w", stderr); -- buggy?
return TRUE;
}
void
ErrorExit(
LPWSTR lpszFunction
)
{
// Retrieve the system error message for the last error code.
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD lerr = GetLastError();
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
lerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Print the error message to the console.
wprintf(L"%s: %s", lpszFunction, (wchar_t *)lpMsgBuf);
// TODO: Update the below to use standard C functions where possible.
// Display the error message in a message box.
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCWSTR)lpMsgBuf) +
lstrlen((LPCWSTR)lpszFunction) + 40) * sizeof(WCHAR));
snwprintf((LPWSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(WCHAR),
L"%s failed with error %d: %s",
lpszFunction, lerr, lpMsgBuf);
MessageBox(NULL, L"Message", L"Error", MB_OK);
LocalFree(lpMsgBuf);
ExitProcess(lerr);
}