-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdtc.cpp
245 lines (213 loc) · 8.55 KB
/
dtc.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
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
#include <windows.h>
#pragma comment (lib, "kernel32")
#pragma comment (lib, "user32")
#pragma comment (lib, "gdi32")
struct the_baby
{
DWORD dwExStyle;
LPCWSTR lpClassName;
LPCWSTR lpWindowName;
DWORD dwStyle;
int X;
int Y;
int nWidth;
int nHeight;
HWND hWndParent;
HMENU hMenu;
HINSTANCE hInstance;
LPVOID lpParam;
};
#define CREATE_DANGEROUS_WINDOW (WM_USER + 0x1337)
#define DESTROY_DANGEROUS_WINDOW (WM_USER + 0x1338)
static DWORD MainThreadID;
static LRESULT CALLBACK ServiceWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{
/* NOTE(casey): This is not really a window handler per se, it's actually just
a remote thread call handler. Windows only really has blocking remote thread
calls if you register a WndProc for them, so that's what we do.
This handles CREATE_DANGEROUS_WINDOW and DESTROY_DANGEROUS_WINDOW, which are
just calls that do CreateWindow and DestroyWindow here on this thread when
some other thread wants that to happen.
*/
LRESULT Result = 0;
switch (Message)
{
case CREATE_DANGEROUS_WINDOW:
{
the_baby *Baby = (the_baby *)WParam;
Result = (LRESULT)CreateWindowExW(Baby->dwExStyle,
Baby->lpClassName,
Baby->lpWindowName,
Baby->dwStyle,
Baby->X,
Baby->Y,
Baby->nWidth,
Baby->nHeight,
Baby->hWndParent,
Baby->hMenu,
Baby->hInstance,
Baby->lpParam);
} break;
case DESTROY_DANGEROUS_WINDOW:
{
DestroyWindow((HWND)WParam);
} break;
default:
{
Result = DefWindowProcW(Window, Message, WParam, LParam);
} break;
}
return Result;
}
static LRESULT CALLBACK DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{
/* NOTE(casey): This is an example of an actual window procedure. It doesn't do anything
but forward things to the main thread, because again, all window messages now occur
on the message thread, and presumably we would rather handle everything there. You
don't _have_ to do that - you could choose to handle some of the messages here.
But if you did, you would have to actually think about whether there are race conditions
with your main thread and all that. So just PostThreadMessageW()'ing everything gets
you out of having to think about it.
*/
LRESULT Result = 0;
switch (Message)
{
// NOTE(casey): Mildly annoying, if you want to specify a window, you have
// to snuggle the params yourself, because Windows doesn't let you forward
// a god damn window message even though the program IS CALLED WINDOWS. It's
// in the name! Let me pass it!
case WM_CLOSE:
{
PostThreadMessageW(MainThreadID, Message, (WPARAM)Window, LParam);
} break;
// NOTE(casey): Anything you want the application to handle, forward to the main thread
// here.
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_DESTROY:
case WM_CHAR:
{
PostThreadMessageW(MainThreadID, Message, WParam, LParam);
} break;
default:
{
Result = DefWindowProcW(Window, Message, WParam, LParam);
} break;
}
return Result;
}
static DWORD WINAPI MainThread(LPVOID Param)
{
/* NOTE(Casey): This is your app code. Basically you just do everything the same,
but instead of calling CreateWindow/DestroyWindow, you use SendMessage to
do it on the other thread, using the CREATE_DANGEROUS_WINDOW and DESTROY_DANGEROUS_WINDOW
user messages. Otherwise, everything proceeds as normal.
*/
HWND ServiceWindow = (HWND)Param;
WNDCLASSEXW WindowClass = {};
WindowClass.cbSize = sizeof(WindowClass);
WindowClass.lpfnWndProc = &DisplayWndProc;
WindowClass.hInstance = GetModuleHandleW(NULL);
WindowClass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
WindowClass.hCursor = LoadCursorA(NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WindowClass.lpszClassName = L"Dangerous Class";
RegisterClassExW(&WindowClass);
the_baby Baby = {};
Baby.dwExStyle = 0;;
Baby.lpClassName = WindowClass.lpszClassName;
Baby.lpWindowName = L"Dangerous Window";
Baby.dwStyle = WS_OVERLAPPEDWINDOW|WS_VISIBLE;
Baby.X = CW_USEDEFAULT;
Baby.Y = CW_USEDEFAULT;
Baby.nWidth = CW_USEDEFAULT;
Baby.nHeight = CW_USEDEFAULT;
Baby.hInstance = WindowClass.hInstance;
HWND ThisWouldBeTheHandleIfYouCared = (HWND)SendMessageW(ServiceWindow, CREATE_DANGEROUS_WINDOW, (WPARAM)&Baby, 0);
int X = 0;
for(;;)
{
MSG Message;
while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
{
switch(Message.message)
{
case WM_CHAR:
{
SendMessageW(ServiceWindow, CREATE_DANGEROUS_WINDOW, (WPARAM)&Baby, 0);
} break;
case WM_CLOSE:
{
SendMessageW(ServiceWindow, DESTROY_DANGEROUS_WINDOW, Message.wParam, 0);
} break;
}
}
int MidPoint = (X++%(64*1024))/64;
int WindowCount = 0;
for(HWND Window = FindWindowExW(0, 0, WindowClass.lpszClassName, 0);
Window;
Window = FindWindowExW(0, Window, WindowClass.lpszClassName, 0))
{
RECT Client;
GetClientRect(Window, &Client);
HDC DC = GetDC(Window);
PatBlt(DC, 0, 0, MidPoint, Client.bottom, BLACKNESS);
if(Client.right > MidPoint)
{
PatBlt(DC, MidPoint, 0, Client.right - MidPoint, Client.bottom, WHITENESS);
}
ReleaseDC(Window, DC);
++WindowCount;
}
if(WindowCount == 0)
{
break;
}
}
ExitProcess(0);
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
/* NOTE(casey): At startup, you create one hidden window used to handle requests
to create or destroy windows. There's nothing special about this window; it
only exists because Windows doesn't have a way to do a remote thread call without
a window handler. You could instead just do this with your own synchronization
primitives if you wanted - this is just the easiest way to do it on Windows
because they've already built it for you.
*/
WNDCLASSEXW WindowClass = {};
WindowClass.cbSize = sizeof(WindowClass);
WindowClass.lpfnWndProc = &ServiceWndProc;
WindowClass.hInstance = GetModuleHandleW(NULL);
WindowClass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
WindowClass.hCursor = LoadCursorA(NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WindowClass.lpszClassName = L"DTCClass";
RegisterClassExW(&WindowClass);
HWND ServiceWindow = CreateWindowExW(0, WindowClass.lpszClassName, L"DTCService", 0,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, WindowClass.hInstance, 0);
// NOTE(casey): Once the service window is created, you can start the main thread,
// which is where all your app code would actually happen.
CreateThread(0, 0, MainThread, ServiceWindow, 0, &MainThreadID);
// NOTE(casey): This thread can just idle for the rest of the run, forwarding
// messages to the main thread that it thinks the main thread wants.
for(;;)
{
MSG Message;
GetMessageW(&Message, 0, 0, 0);
TranslateMessage(&Message);
if((Message.message == WM_CHAR) ||
(Message.message == WM_KEYDOWN) ||
(Message.message == WM_QUIT) ||
(Message.message == WM_SIZE))
{
PostThreadMessageW(MainThreadID, Message.message, Message.wParam, Message.lParam);
}
else
{
DispatchMessageW(&Message);
}
}
}