graphics-hook.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <windows.h>
  3. #include <shlobj.h>
  4. #include <psapi.h>
  5. #include "graphics-hook.h"
  6. #include "../obfuscate.h"
  7. #include "../funchook.h"
  8. #define DEBUG_OUTPUT
  9. #ifdef DEBUG_OUTPUT
  10. #define DbgOut(x) OutputDebugStringA(x)
  11. #else
  12. #define DbgOut(x)
  13. #endif
  14. struct thread_data {
  15. CRITICAL_SECTION mutexes[NUM_BUFFERS];
  16. CRITICAL_SECTION data_mutex;
  17. void *volatile cur_data;
  18. uint8_t *shmem_textures[2];
  19. HANDLE copy_thread;
  20. HANDLE copy_event;
  21. HANDLE stop_event;
  22. volatile int cur_tex;
  23. unsigned int pitch;
  24. unsigned int cy;
  25. volatile bool locked_textures[NUM_BUFFERS];
  26. };
  27. ipc_pipe_client_t pipe = {0};
  28. HANDLE signal_restart = NULL;
  29. HANDLE signal_stop = NULL;
  30. HANDLE signal_ready = NULL;
  31. HANDLE signal_exit = NULL;
  32. HANDLE tex_mutexes[2] = {NULL, NULL};
  33. static HANDLE filemap_hook_info = NULL;
  34. static volatile bool stop_loop = false;
  35. static HANDLE capture_thread = NULL;
  36. char system_path[MAX_PATH] = {0};
  37. char process_name[MAX_PATH] = {0};
  38. char keepalive_name[64] = {0};
  39. static unsigned int shmem_id_counter = 0;
  40. static void *shmem_info = NULL;
  41. static HANDLE shmem_file_handle = 0;
  42. static struct thread_data thread_data = {0};
  43. volatile bool active = false;
  44. struct hook_info *global_hook_info = NULL;
  45. static inline void wait_for_dll_main_finish(HANDLE thread_handle)
  46. {
  47. if (thread_handle) {
  48. WaitForSingleObject(thread_handle, 100);
  49. CloseHandle(thread_handle);
  50. }
  51. }
  52. bool init_pipe(void)
  53. {
  54. char new_name[64];
  55. sprintf(new_name, "%s%lu", PIPE_NAME, GetCurrentProcessId());
  56. if (!ipc_pipe_client_open(&pipe, new_name)) {
  57. DbgOut("Failed to open pipe\n");
  58. return false;
  59. }
  60. return true;
  61. }
  62. static HANDLE init_event(const char *name, DWORD pid)
  63. {
  64. HANDLE handle = get_event_plus_id(name, pid);
  65. if (!handle)
  66. hlog("Failed to get event '%s': %lu", name, GetLastError());
  67. return handle;
  68. }
  69. static HANDLE init_mutex(const char *name, DWORD pid)
  70. {
  71. char new_name[64];
  72. HANDLE handle;
  73. sprintf(new_name, "%s%lu", name, pid);
  74. handle = OpenMutexA(MUTEX_ALL_ACCESS, false, new_name);
  75. if (!handle)
  76. hlog("Failed to open mutex '%s': %lu", name, GetLastError());
  77. return handle;
  78. }
  79. static inline bool init_signals(void)
  80. {
  81. DWORD pid = GetCurrentProcessId();
  82. signal_restart = init_event(EVENT_CAPTURE_RESTART, pid);
  83. if (!signal_restart) {
  84. return false;
  85. }
  86. signal_stop = init_event(EVENT_CAPTURE_STOP, pid);
  87. if (!signal_stop) {
  88. return false;
  89. }
  90. signal_ready = init_event(EVENT_HOOK_READY, pid);
  91. if (!signal_ready) {
  92. return false;
  93. }
  94. signal_exit = init_event(EVENT_HOOK_EXIT, pid);
  95. if (!signal_exit) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. static inline bool init_mutexes(void)
  101. {
  102. DWORD pid = GetCurrentProcessId();
  103. tex_mutexes[0] = init_mutex(MUTEX_TEXTURE1, pid);
  104. if (!tex_mutexes[0]) {
  105. return false;
  106. }
  107. tex_mutexes[1] = init_mutex(MUTEX_TEXTURE2, pid);
  108. if (!tex_mutexes[1]) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. static inline bool init_system_path(void)
  114. {
  115. HRESULT hr = SHGetFolderPathA(NULL, CSIDL_SYSTEM, NULL,
  116. SHGFP_TYPE_CURRENT, system_path);
  117. if (hr != S_OK) {
  118. hlog("Failed to get windows system path: %08lX", hr);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static inline void log_current_process(void)
  124. {
  125. DWORD len = GetModuleBaseNameA(GetCurrentProcess(), NULL, process_name,
  126. MAX_PATH);
  127. if (len > 0) {
  128. process_name[len] = 0;
  129. hlog("Hooked to process: %s", process_name);
  130. }
  131. hlog("(half life scientist) everything.. seems to be in order");
  132. }
  133. static inline bool init_hook_info(void)
  134. {
  135. filemap_hook_info = get_hook_info(GetCurrentProcessId());
  136. if (!filemap_hook_info) {
  137. hlog("Failed to create hook info file mapping: %lu",
  138. GetLastError());
  139. return false;
  140. }
  141. global_hook_info = MapViewOfFile(filemap_hook_info, FILE_MAP_ALL_ACCESS,
  142. 0, 0, sizeof(struct hook_info));
  143. if (!global_hook_info) {
  144. hlog("Failed to map the hook info file mapping: %lu",
  145. GetLastError());
  146. return false;
  147. }
  148. return true;
  149. }
  150. static inline bool init_hook(HANDLE thread_handle)
  151. {
  152. wait_for_dll_main_finish(thread_handle);
  153. sprintf(keepalive_name, "%s%lu", EVENT_HOOK_KEEPALIVE,
  154. GetCurrentProcessId());
  155. if (!init_pipe()) {
  156. return false;
  157. }
  158. if (!init_signals()) {
  159. return false;
  160. }
  161. if (!init_mutexes()) {
  162. return false;
  163. }
  164. if (!init_system_path()) {
  165. return false;
  166. }
  167. if (!init_hook_info()) {
  168. return false;
  169. }
  170. log_current_process();
  171. SetEvent(signal_restart);
  172. return true;
  173. }
  174. static inline void close_handle(HANDLE *handle)
  175. {
  176. if (*handle) {
  177. CloseHandle(*handle);
  178. *handle = NULL;
  179. }
  180. }
  181. static void free_hook(void)
  182. {
  183. if (filemap_hook_info) {
  184. CloseHandle(filemap_hook_info);
  185. filemap_hook_info = NULL;
  186. }
  187. if (global_hook_info) {
  188. UnmapViewOfFile(global_hook_info);
  189. global_hook_info = NULL;
  190. }
  191. close_handle(&tex_mutexes[1]);
  192. close_handle(&tex_mutexes[0]);
  193. close_handle(&signal_exit);
  194. close_handle(&signal_ready);
  195. close_handle(&signal_stop);
  196. close_handle(&signal_restart);
  197. ipc_pipe_client_free(&pipe);
  198. }
  199. static inline bool d3d8_hookable(void)
  200. {
  201. return !!global_hook_info->offsets.d3d8.present &&
  202. !!global_hook_info->offsets.d3d8.reset;
  203. }
  204. static inline bool ddraw_hookable(void)
  205. {
  206. return !!global_hook_info->offsets.ddraw.surface_create &&
  207. !!global_hook_info->offsets.ddraw.surface_restore &&
  208. !!global_hook_info->offsets.ddraw.surface_release &&
  209. !!global_hook_info->offsets.ddraw.surface_unlock &&
  210. !!global_hook_info->offsets.ddraw.surface_blt &&
  211. !!global_hook_info->offsets.ddraw.surface_flip &&
  212. !!global_hook_info->offsets.ddraw.surface_set_palette &&
  213. !!global_hook_info->offsets.ddraw.palette_set_entries;
  214. }
  215. static inline bool d3d9_hookable(void)
  216. {
  217. return !!global_hook_info->offsets.d3d9.present &&
  218. !!global_hook_info->offsets.d3d9.present_ex &&
  219. !!global_hook_info->offsets.d3d9.present_swap &&
  220. !!global_hook_info->offsets.d3d9.reset &&
  221. !!global_hook_info->offsets.d3d9.reset_ex;
  222. }
  223. static inline bool dxgi_hookable(void)
  224. {
  225. return !!global_hook_info->offsets.dxgi.present &&
  226. !!global_hook_info->offsets.dxgi.resize;
  227. }
  228. static inline bool attempt_hook(void)
  229. {
  230. //static bool ddraw_hooked = false;
  231. static bool d3d8_hooked = false;
  232. static bool d3d9_hooked = false;
  233. static bool dxgi_hooked = false;
  234. static bool gl_hooked = false;
  235. if (!d3d9_hooked) {
  236. if (!d3d9_hookable()) {
  237. d3d9_hooked = true;
  238. } else {
  239. d3d9_hooked = hook_d3d9();
  240. if (d3d9_hooked) {
  241. return true;
  242. }
  243. }
  244. }
  245. if (!dxgi_hooked) {
  246. if (!dxgi_hookable()) {
  247. dxgi_hooked = true;
  248. } else {
  249. dxgi_hooked = hook_dxgi();
  250. if (dxgi_hooked) {
  251. return true;
  252. }
  253. }
  254. }
  255. if (!gl_hooked) {
  256. gl_hooked = hook_gl();
  257. if (gl_hooked) {
  258. return true;
  259. }
  260. /*} else {
  261. rehook_gl();*/
  262. }
  263. if (!d3d8_hooked) {
  264. if (!d3d8_hookable()) {
  265. d3d8_hooked = true;
  266. } else {
  267. d3d8_hooked = hook_d3d8();
  268. if (d3d8_hooked) {
  269. return true;
  270. }
  271. }
  272. }
  273. /*if (!ddraw_hooked) {
  274. if (!ddraw_hookable()) {
  275. ddraw_hooked = true;
  276. } else {
  277. ddraw_hooked = hook_ddraw();
  278. if (ddraw_hooked) {
  279. return true;
  280. }
  281. }
  282. }*/
  283. return false;
  284. }
  285. static inline void capture_loop(void)
  286. {
  287. while (!attempt_hook())
  288. Sleep(40);
  289. for (size_t n = 0; !stop_loop; n++) {
  290. /* this causes it to check every 4 seconds, but still with
  291. * a small sleep interval in case the thread needs to stop */
  292. if (n % 100 == 0) attempt_hook();
  293. Sleep(40);
  294. }
  295. }
  296. static DWORD WINAPI main_capture_thread(HANDLE thread_handle)
  297. {
  298. if (!init_hook(thread_handle)) {
  299. DbgOut("Failed to init hook\n");
  300. free_hook();
  301. return 0;
  302. }
  303. capture_loop();
  304. return 0;
  305. }
  306. static inline void hlogv(const char *format, va_list args)
  307. {
  308. char message[1024] = "";
  309. int num = _vsprintf_p(message, 1024, format, args);
  310. if (num) {
  311. if (!ipc_pipe_client_write(&pipe, message, num + 1)) {
  312. ipc_pipe_client_free(&pipe);
  313. }
  314. DbgOut(message);
  315. DbgOut("\n");
  316. }
  317. }
  318. void hlog(const char *format, ...)
  319. {
  320. va_list args;
  321. va_start(args, format);
  322. hlogv(format, args);
  323. va_end(args);
  324. }
  325. void hlog_hr(const char *text, HRESULT hr)
  326. {
  327. LPSTR buffer = NULL;
  328. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  329. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  330. FORMAT_MESSAGE_IGNORE_INSERTS,
  331. NULL, hr, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  332. (LPSTR)&buffer, 0, NULL);
  333. if (buffer) {
  334. hlog("%s (0x%08lX): %s", text, hr, buffer);
  335. LocalFree(buffer);
  336. } else {
  337. hlog("%s (0x%08lX)", text, hr);
  338. }
  339. }
  340. static inline uint64_t get_clockfreq(void)
  341. {
  342. static bool have_clockfreq = false;
  343. static LARGE_INTEGER clock_freq;
  344. if (!have_clockfreq) {
  345. QueryPerformanceFrequency(&clock_freq);
  346. have_clockfreq = true;
  347. }
  348. return clock_freq.QuadPart;
  349. }
  350. uint64_t os_gettime_ns(void)
  351. {
  352. LARGE_INTEGER current_time;
  353. double time_val;
  354. QueryPerformanceCounter(&current_time);
  355. time_val = (double)current_time.QuadPart;
  356. time_val *= 1000000000.0;
  357. time_val /= (double)get_clockfreq();
  358. return (uint64_t)time_val;
  359. }
  360. static inline int try_lock_shmem_tex(int id)
  361. {
  362. int next = id == 0 ? 1 : 0;
  363. if (WaitForSingleObject(tex_mutexes[id], 0) == WAIT_OBJECT_0) {
  364. return id;
  365. } else if (WaitForSingleObject(tex_mutexes[next], 0) == WAIT_OBJECT_0) {
  366. return next;
  367. }
  368. return -1;
  369. }
  370. static inline void unlock_shmem_tex(int id)
  371. {
  372. if (id != -1) {
  373. ReleaseMutex(tex_mutexes[id]);
  374. }
  375. }
  376. static inline bool init_shared_info(size_t size)
  377. {
  378. char name[64];
  379. sprintf_s(name, 64, "%s%u", SHMEM_TEXTURE, ++shmem_id_counter);
  380. shmem_file_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
  381. PAGE_READWRITE, 0, (DWORD)size, name);
  382. if (!shmem_file_handle) {
  383. hlog("init_shared_info: Failed to create shared memory: %d",
  384. GetLastError());
  385. return false;
  386. }
  387. shmem_info = MapViewOfFile(shmem_file_handle, FILE_MAP_ALL_ACCESS,
  388. 0, 0, size);
  389. if (!shmem_info) {
  390. hlog("init_shared_info: Failed to map shared memory: %d",
  391. GetLastError());
  392. return false;
  393. }
  394. return true;
  395. }
  396. bool capture_init_shtex(struct shtex_data **data, HWND window,
  397. uint32_t base_cx, uint32_t base_cy, uint32_t cx, uint32_t cy,
  398. uint32_t format, bool flip, uintptr_t handle)
  399. {
  400. if (!init_shared_info(sizeof(struct shtex_data))) {
  401. hlog("capture_init_shtex: Failed to initialize memory");
  402. return false;
  403. }
  404. *data = shmem_info;
  405. (*data)->tex_handle = (uint32_t)handle;
  406. global_hook_info->window = (uint32_t)(uintptr_t)window;
  407. global_hook_info->type = CAPTURE_TYPE_TEXTURE;
  408. global_hook_info->format = format;
  409. global_hook_info->flip = flip;
  410. global_hook_info->map_id = shmem_id_counter;
  411. global_hook_info->map_size = sizeof(struct shtex_data);
  412. global_hook_info->cx = cx;
  413. global_hook_info->cy = cy;
  414. global_hook_info->base_cx = base_cx;
  415. global_hook_info->base_cy = base_cy;
  416. if (!SetEvent(signal_ready)) {
  417. hlog("capture_init_shtex: Failed to signal ready: %d",
  418. GetLastError());
  419. return false;
  420. }
  421. active = true;
  422. return true;
  423. }
  424. static DWORD CALLBACK copy_thread(LPVOID unused)
  425. {
  426. uint32_t pitch = thread_data.pitch;
  427. uint32_t cy = thread_data.cy;
  428. HANDLE events[2] = {NULL, NULL};
  429. int shmem_id = 0;
  430. if (!duplicate_handle(&events[0], thread_data.copy_event)) {
  431. hlog_hr("copy_thread: Failed to duplicate copy event: %d",
  432. GetLastError());
  433. return 0;
  434. }
  435. if (!duplicate_handle(&events[1], thread_data.stop_event)) {
  436. hlog_hr("copy_thread: Failed to duplicate stop event: %d",
  437. GetLastError());
  438. goto finish;
  439. }
  440. for (;;) {
  441. int copy_tex;
  442. void *cur_data;
  443. DWORD ret = WaitForMultipleObjects(2, events, false, INFINITE);
  444. if (ret != WAIT_OBJECT_0) {
  445. break;
  446. }
  447. EnterCriticalSection(&thread_data.data_mutex);
  448. copy_tex = thread_data.cur_tex;
  449. cur_data = thread_data.cur_data;
  450. LeaveCriticalSection(&thread_data.data_mutex);
  451. if (copy_tex < NUM_BUFFERS && !!cur_data) {
  452. EnterCriticalSection(&thread_data.mutexes[copy_tex]);
  453. int lock_id = try_lock_shmem_tex(shmem_id);
  454. if (lock_id != -1) {
  455. memcpy(thread_data.shmem_textures[lock_id],
  456. cur_data, pitch * cy);
  457. unlock_shmem_tex(lock_id);
  458. ((struct shmem_data*)shmem_info)->last_tex =
  459. lock_id;
  460. shmem_id = lock_id == 0 ? 1 : 0;
  461. }
  462. LeaveCriticalSection(&thread_data.mutexes[copy_tex]);
  463. }
  464. }
  465. finish:
  466. for (size_t i = 0; i < 2; i++) {
  467. if (events[i]) {
  468. CloseHandle(events[i]);
  469. }
  470. }
  471. (void)unused;
  472. return 0;
  473. }
  474. void shmem_copy_data(size_t idx, void *volatile data)
  475. {
  476. EnterCriticalSection(&thread_data.data_mutex);
  477. thread_data.cur_tex = (int)idx;
  478. thread_data.cur_data = data;
  479. thread_data.locked_textures[idx] = true;
  480. LeaveCriticalSection(&thread_data.data_mutex);
  481. SetEvent(thread_data.copy_event);
  482. }
  483. bool shmem_texture_data_lock(int idx)
  484. {
  485. bool locked;
  486. EnterCriticalSection(&thread_data.data_mutex);
  487. locked = thread_data.locked_textures[idx];
  488. LeaveCriticalSection(&thread_data.data_mutex);
  489. if (locked) {
  490. EnterCriticalSection(&thread_data.mutexes[idx]);
  491. return true;
  492. }
  493. return false;
  494. }
  495. void shmem_texture_data_unlock(int idx)
  496. {
  497. EnterCriticalSection(&thread_data.data_mutex);
  498. thread_data.locked_textures[idx] = false;
  499. LeaveCriticalSection(&thread_data.data_mutex);
  500. LeaveCriticalSection(&thread_data.mutexes[idx]);
  501. }
  502. static inline bool init_shmem_thread(uint32_t pitch, uint32_t cy)
  503. {
  504. struct shmem_data *data = shmem_info;
  505. thread_data.pitch = pitch;
  506. thread_data.cy = cy;
  507. thread_data.shmem_textures[0] = (uint8_t*)data + data->tex1_offset;
  508. thread_data.shmem_textures[1] = (uint8_t*)data + data->tex2_offset;
  509. thread_data.copy_event = CreateEvent(NULL, false, false, NULL);
  510. if (!thread_data.copy_event) {
  511. hlog("init_shmem_thread: Failed to create copy event: %d",
  512. GetLastError());
  513. return false;
  514. }
  515. thread_data.stop_event = CreateEvent(NULL, true, false, NULL);
  516. if (!thread_data.stop_event) {
  517. hlog("init_shmem_thread: Failed to create stop event: %d",
  518. GetLastError());
  519. return false;
  520. }
  521. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  522. InitializeCriticalSection(&thread_data.mutexes[i]);
  523. }
  524. InitializeCriticalSection(&thread_data.data_mutex);
  525. thread_data.copy_thread = CreateThread(NULL, 0, copy_thread, NULL, 0,
  526. NULL);
  527. if (!thread_data.copy_thread) {
  528. hlog("init_shmem_thread: Failed to create thread: %d",
  529. GetLastError());
  530. return false;
  531. }
  532. return true;
  533. }
  534. #ifndef ALIGN
  535. #define ALIGN(bytes, align) (((bytes) + ((align) - 1)) & ~((align) - 1))
  536. #endif
  537. bool capture_init_shmem(struct shmem_data **data, HWND window,
  538. uint32_t base_cx, uint32_t base_cy, uint32_t cx, uint32_t cy,
  539. uint32_t pitch, uint32_t format, bool flip)
  540. {
  541. uint32_t tex_size = cy * pitch;
  542. uint32_t aligned_header = ALIGN(sizeof(struct shmem_data), 32);
  543. uint32_t aligned_tex = ALIGN(tex_size, 32);
  544. uint32_t total_size = aligned_header + aligned_tex * 2;
  545. uintptr_t align_pos;
  546. if (!init_shared_info(total_size)) {
  547. hlog("capture_init_shmem: Failed to initialize memory");
  548. return false;
  549. }
  550. *data = shmem_info;
  551. /* to ensure fast copy rate, align texture data to 256bit addresses */
  552. align_pos = (uintptr_t)shmem_info;
  553. align_pos += aligned_header;
  554. align_pos &= ~(32 - 1);
  555. align_pos -= (uintptr_t)shmem_info;
  556. (*data)->last_tex = -1;
  557. (*data)->tex1_offset = (uint32_t)align_pos;
  558. (*data)->tex2_offset = (*data)->tex1_offset + aligned_tex;
  559. global_hook_info->window = (uint32_t)(uintptr_t)window;
  560. global_hook_info->type = CAPTURE_TYPE_MEMORY;
  561. global_hook_info->format = format;
  562. global_hook_info->flip = flip;
  563. global_hook_info->map_id = shmem_id_counter;
  564. global_hook_info->map_size = total_size;
  565. global_hook_info->pitch = pitch;
  566. global_hook_info->cx = cx;
  567. global_hook_info->cy = cy;
  568. global_hook_info->base_cx = base_cx;
  569. global_hook_info->base_cy = base_cy;
  570. if (!init_shmem_thread(pitch, cy)) {
  571. return false;
  572. }
  573. if (!SetEvent(signal_ready)) {
  574. hlog("capture_init_shmem: Failed to signal ready: %d",
  575. GetLastError());
  576. return false;
  577. }
  578. active = true;
  579. return true;
  580. }
  581. static inline void thread_data_free(void)
  582. {
  583. if (thread_data.copy_thread) {
  584. DWORD ret;
  585. SetEvent(thread_data.stop_event);
  586. ret = WaitForSingleObject(thread_data.copy_thread, 500);
  587. if (ret != WAIT_OBJECT_0)
  588. TerminateThread(thread_data.copy_thread, (DWORD)-1);
  589. CloseHandle(thread_data.copy_thread);
  590. }
  591. if (thread_data.stop_event)
  592. CloseHandle(thread_data.stop_event);
  593. if (thread_data.copy_event)
  594. CloseHandle(thread_data.copy_event);
  595. for (size_t i = 0; i < NUM_BUFFERS; i++)
  596. DeleteCriticalSection(&thread_data.mutexes[i]);
  597. DeleteCriticalSection(&thread_data.data_mutex);
  598. memset(&thread_data, 0, sizeof(thread_data));
  599. }
  600. void capture_free(void)
  601. {
  602. thread_data_free();
  603. if (shmem_info) {
  604. UnmapViewOfFile(shmem_info);
  605. shmem_info = NULL;
  606. }
  607. close_handle(&shmem_file_handle);
  608. SetEvent(signal_restart);
  609. active = false;
  610. }
  611. BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID unused1)
  612. {
  613. if (reason == DLL_PROCESS_ATTACH) {
  614. wchar_t name[MAX_PATH];
  615. HANDLE cur_thread = OpenThread(THREAD_ALL_ACCESS, false,
  616. GetCurrentThreadId());
  617. /* this prevents the library from being automatically unloaded
  618. * by the next FreeLibrary call */
  619. GetModuleFileNameW(hinst, name, MAX_PATH);
  620. LoadLibraryW(name);
  621. capture_thread = CreateThread(NULL, 0,
  622. (LPTHREAD_START_ROUTINE)main_capture_thread,
  623. (LPVOID)cur_thread, 0, 0);
  624. if (!capture_thread) {
  625. CloseHandle(cur_thread);
  626. return false;
  627. }
  628. } else if (reason == DLL_PROCESS_DETACH) {
  629. if (capture_thread) {
  630. stop_loop = true;
  631. WaitForSingleObject(capture_thread, 300);
  632. CloseHandle(capture_thread);
  633. }
  634. free_hook();
  635. }
  636. (void)unused1;
  637. return true;
  638. }
  639. __declspec(dllexport) LRESULT CALLBACK dummy_debug_proc(int code,
  640. WPARAM wparam, LPARAM lparam)
  641. {
  642. static bool hooking = true;
  643. MSG *msg = (MSG*)lparam;
  644. if (hooking && msg->message == (WM_USER + 432)) {
  645. HMODULE user32 = GetModuleHandleW(L"USER32");
  646. BOOL (WINAPI *unhook_windows_hook_ex)(HHOOK) = NULL;
  647. unhook_windows_hook_ex = get_obfuscated_func(user32,
  648. "VojeleY`bdgxvM`hhDz",
  649. 0x7F55F80C9EE3A213ULL);
  650. if (unhook_windows_hook_ex)
  651. unhook_windows_hook_ex((HHOOK)msg->lParam);
  652. hooking = false;
  653. }
  654. return CallNextHookEx(0, code, wparam, lparam);
  655. }