-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcond_clk_lib_win32.cpp
More file actions
executable file
·321 lines (275 loc) · 6.11 KB
/
Copy pathcond_clk_lib_win32.cpp
File metadata and controls
executable file
·321 lines (275 loc) · 6.11 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
// cond_clk_lib.cpp : 定义 DLL 应用程序的导出函数。
//
#include <vector>
#include <iterator>
#include <Windows.h>
#include <d3d9.h>
#pragma comment(lib,"d3d9.lib")
#include <WinError.h>
#define DLL_EXPORT extern "C" __declspec(dllexport)
UINT Width = 0, Height = 0;
IDirect3D9* pD3DScreenCapture = NULL;
IDirect3DDevice9* pd3dDevice = NULL;
IDirect3DSurface9* pSurface = NULL;
DLL_EXPORT void reset(void) {
Width = Height = 0;
if (pSurface) {
pSurface->Release();
pSurface = NULL;
}
if (pd3dDevice) {
pd3dDevice->Release();
pd3dDevice = NULL;
}
if (pD3DScreenCapture) {
pD3DScreenCapture->Release();
pD3DScreenCapture = NULL;
}
}
#pragma comment(linker,"/export:cleanup=reset")
bool initialize(void) {
reset();
HWND hWnd = NULL;// GetDesktopWindow();
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
do {
if ((pD3DScreenCapture = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
break;
if (FAILED(pD3DScreenCapture->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &ddm)))
break;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = TRUE;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat = ddm.Format;
d3dpp.BackBufferHeight = Height = ddm.Height;
d3dpp.BackBufferWidth = Width = ddm.Width;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hWnd;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz = /*ddm.RefreshRate*/D3DPRESENT_RATE_DEFAULT;
HRESULT res;
if (FAILED(res = pD3DScreenCapture->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))
break;
if (FAILED(pd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL)))
break;
return true;
} while (false);
return false;
}
BOOL APIENTRY DllMain(HMODULE,
DWORD,
LPVOID
)
{
return TRUE;
}
DLL_EXPORT UINT width(void) {
if (NULL == pSurface)
initialize();
return Width;
}
DLL_EXPORT UINT height(void) {
if (NULL == pSurface)
initialize();
return Height;
}
DLL_EXPORT ULONG capture(PVOID buffer, ULONG limit) {
if (NULL == pSurface) {
if (!initialize())
return 0;
}
ULONG required_size = 4 * Width*Height;
if (limit < required_size)
return required_size;
do {
pd3dDevice->GetFrontBufferData(0, pSurface);
D3DLOCKED_RECT lockedRect;
RECT area = { 0,0,(LONG)Width,(LONG)Height };
if (FAILED(pSurface->LockRect(&lockedRect, &area, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY))) {
required_size = 0;
break;
}
if (Width * 4 != (UINT)lockedRect.Pitch) {
required_size = 0;
break;
}
RtlMoveMemory(buffer, lockedRect.pBits, required_size);
} while (false);
pSurface->UnlockRect();
return required_size;
}
#pragma warning(disable : 4309)
template<typename Out>
bool translate(Out out, char c) {
if (isprint(c) || c == '\t' || c == '\n' || c == 27)
;
else
return false;
bool shift = true;
switch (c) {
case '~':
c = '`';
break;
case '!':
c = '1';
break;
case '@':
c = '2';
break;
case '#':
c = '3';
break;
case '$':
c = '4';
break;
case '%':
c = '5';
break;
case '^':
c = '6';
break;
case '&':
c = '7';
break;
case '*':
c = '8';
break;
case '(':
c = '9';
break;
case ')':
c = '0';
break;
case '_':
c = '-';
break;
case '+':
c = '=';
break;
case '<':
c = ',';
break;
case '>':
c = '.';
break;
case '?':
c = '/';
break;
case ':':
c = ';';
break;
case '\"':
c = '\'';
break;
case '{':
c = '[';
break;
case '}':
c = ']';
break;
case '|':
c = '\\';
break;
default:
shift = false;
}
if (c >= 'A' && c <= 'Z')
shift = true;
if (c >= 'a' && c <= 'z')
c -= 0x20;
switch (c) {
case '\'':
c = 0xDE;
break;
case ',':
c = 0xBC;
break;
case '-':
c = 0xBD;
break;
case '.':
c = 0xBE;
break;
case '/':
c = 0xBF;
break;
case ';':
c = 0xBA;
break;
case '=':
c = 0xBB; //???
break;
case '[':
c = 0xDB;
break;
case '\\':
c = 0xDC;
break;
case ']':
c = 0xDD;
break;
case '`':
c = 0xC0;
break;
case '\n':
c = 0x0D;
break;
}
INPUT cur = { 1,0 };
if (shift) {
cur.ki.wVk = VK_SHIFT;
*out++ = cur;
}
cur.ki.wVk = c;
*out++ = cur;
cur.ki.dwFlags = KEYEVENTF_KEYUP;
*out++ = cur;
if (shift) {
cur.ki.wVk = VK_SHIFT;
*out++ = cur;
}
return true;
}
DLL_EXPORT UINT type(LPCSTR string) {
std::vector<INPUT> inputs;
UINT count = 0;
while (*string) {
if (!translate(back_inserter(inputs), *string++))
break;
++count;
}
if (inputs.size() != SendInput((UINT)inputs.size(), inputs.data(), sizeof(INPUT)))
return 0;
return count;
}
DLL_EXPORT BOOL move(UINT x, UINT y) {
if (NULL == pSurface) {
if (!initialize())
return FALSE;
}
INPUT input = { 0 };
input.mi.dx = (LONG)(x * ((double)0xFFFF / Width));
input.mi.dy = (LONG)(y * ((double)0xFFFF / Height));
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
return 1 == SendInput(1, &input, sizeof(INPUT)) ? TRUE : FALSE;
}
//DLL_EXPORT BOOL click(UINT x, UINT y,UINT button) {
DLL_EXPORT BOOL click(UINT button) {
if (button > 2)
return FALSE;
static const DWORD down[] = { MOUSEEVENTF_LEFTDOWN,MOUSEEVENTF_RIGHTDOWN ,MOUSEEVENTF_MIDDLEDOWN };
static const DWORD up[] = { MOUSEEVENTF_LEFTUP ,MOUSEEVENTF_RIGHTUP ,MOUSEEVENTF_MIDDLEUP };
INPUT inputs[2] = { 0 };
//TRICK : inputs[i].type==0 => INPUT_MOUSE
inputs[0].mi.dwFlags = down[button];
inputs[1].mi.dwFlags = up[button];
return 2 == SendInput(2, inputs, sizeof(INPUT)) ? TRUE : FALSE;
}
DLL_EXPORT BOOL scroll(int step) {
INPUT input = { 0 };
input.mi.mouseData = (DWORD)(-step);
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
return 1 == SendInput(1, &input, sizeof(INPUT)) ? TRUE : FALSE;
}