obs-windows.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/windows/win-registry.h"
  15. #include "util/windows/win-version.h"
  16. #include "util/platform.h"
  17. #include "util/dstr.h"
  18. #include "obs.h"
  19. #include "obs-internal.h"
  20. #include <windows.h>
  21. static uint32_t win_ver = 0;
  22. const char *get_module_extension(void)
  23. {
  24. return ".dll";
  25. }
  26. #ifdef _WIN64
  27. #define BIT_STRING "64bit"
  28. #else
  29. #define BIT_STRING "32bit"
  30. #endif
  31. static const char *module_bin[] = {
  32. "obs-plugins/" BIT_STRING,
  33. "../../obs-plugins/" BIT_STRING,
  34. };
  35. static const char *module_data[] = {
  36. "data/%module%",
  37. "../../data/obs-plugins/%module%"
  38. };
  39. static const int module_patterns_size =
  40. sizeof(module_bin)/sizeof(module_bin[0]);
  41. void add_default_module_paths(void)
  42. {
  43. for (int i = 0; i < module_patterns_size; i++)
  44. obs_add_module_path(module_bin[i], module_data[i]);
  45. }
  46. /* on windows, points to [base directory]/data/libobs */
  47. char *find_libobs_data_file(const char *file)
  48. {
  49. struct dstr path;
  50. dstr_init(&path);
  51. if (check_path(file, "data/libobs/", &path))
  52. return path.array;
  53. if (check_path(file, "../../data/libobs/", &path))
  54. return path.array;
  55. dstr_free(&path);
  56. return NULL;
  57. }
  58. static void log_processor_info(void)
  59. {
  60. HKEY key;
  61. wchar_t data[1024];
  62. char *str = NULL;
  63. DWORD size, speed;
  64. LSTATUS status;
  65. memset(data, 0, 1024);
  66. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  67. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  68. &key);
  69. if (status != ERROR_SUCCESS)
  70. return;
  71. size = 1024;
  72. status = RegQueryValueExW(key, L"ProcessorNameString", NULL, NULL,
  73. (LPBYTE)data, &size);
  74. if (status == ERROR_SUCCESS) {
  75. os_wcs_to_utf8_ptr(data, 0, &str);
  76. blog(LOG_INFO, "CPU Name: %s", str);
  77. bfree(str);
  78. }
  79. size = sizeof(speed);
  80. status = RegQueryValueExW(key, L"~MHz", NULL, NULL, (LPBYTE)&speed,
  81. &size);
  82. if (status == ERROR_SUCCESS)
  83. blog(LOG_INFO, "CPU Speed: %ldMHz", speed);
  84. RegCloseKey(key);
  85. }
  86. static void log_processor_cores(void)
  87. {
  88. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d",
  89. os_get_physical_cores(), os_get_logical_cores());
  90. }
  91. static void log_available_memory(void)
  92. {
  93. MEMORYSTATUSEX ms;
  94. ms.dwLength = sizeof(ms);
  95. GlobalMemoryStatusEx(&ms);
  96. #ifdef _WIN64
  97. const char *note = "";
  98. #else
  99. const char *note = " (NOTE: 32bit programs cannot use more than 3gb)";
  100. #endif
  101. blog(LOG_INFO, "Physical Memory: %luMB Total, %luMB Free%s",
  102. (DWORD)(ms.ullTotalPhys / 1048576),
  103. (DWORD)(ms.ullAvailPhys / 1048576),
  104. note);
  105. }
  106. static void log_windows_version(void)
  107. {
  108. struct win_version_info ver;
  109. get_win_ver(&ver);
  110. bool b64 = is_64_bit_windows();
  111. const char *windows_bitness = b64 ? "64" : "32";
  112. blog(LOG_INFO, "Windows Version: %d.%d Build %d (revision: %d; %s-bit)",
  113. ver.major, ver.minor, ver.build, ver.revis,
  114. windows_bitness);
  115. }
  116. static void log_admin_status(void)
  117. {
  118. SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
  119. PSID admin_group;
  120. BOOL success;
  121. success = AllocateAndInitializeSid(&auth, 2,
  122. SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  123. 0, 0, 0, 0, 0, 0, &admin_group);
  124. if (success) {
  125. if (!CheckTokenMembership(NULL, admin_group, &success))
  126. success = false;
  127. FreeSid(admin_group);
  128. }
  129. blog(LOG_INFO, "Running as administrator: %s",
  130. success ? "true" : "false");
  131. }
  132. typedef HRESULT (WINAPI *dwm_is_composition_enabled_t)(BOOL*);
  133. static void log_aero(void)
  134. {
  135. dwm_is_composition_enabled_t composition_enabled = NULL;
  136. const char *aeroMessage = win_ver >= 0x602 ?
  137. " (Aero is always on for windows 8 and above)" : "";
  138. HMODULE dwm = LoadLibraryW(L"dwmapi");
  139. BOOL bComposition = true;
  140. if (!dwm) {
  141. return;
  142. }
  143. composition_enabled = (dwm_is_composition_enabled_t)GetProcAddress(dwm,
  144. "DwmIsCompositionEnabled");
  145. if (!composition_enabled) {
  146. return;
  147. }
  148. composition_enabled(&bComposition);
  149. blog(LOG_INFO, "Aero is %s%s", bComposition ? "Enabled" : "Disabled",
  150. aeroMessage);
  151. }
  152. #define WIN10_GAME_BAR_REG_KEY \
  153. L"Software\\Microsoft\\Windows\\CurrentVersion\\GameDVR"
  154. #define WIN10_GAME_DVR_POLICY_REG_KEY \
  155. L"SOFTWARE\\Policies\\Microsoft\\Windows\\GameDVR"
  156. #define WIN10_GAME_DVR_REG_KEY L"System\\GameConfigStore"
  157. #define WIN10_GAME_MODE_REG_KEY L"Software\\Microsoft\\GameBar"
  158. static void log_gaming_features(void)
  159. {
  160. if (win_ver < 0xA00)
  161. return;
  162. struct reg_dword game_bar_enabled;
  163. struct reg_dword game_dvr_allowed;
  164. struct reg_dword game_dvr_enabled;
  165. struct reg_dword game_dvr_bg_recording;
  166. struct reg_dword game_mode_enabled;
  167. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_BAR_REG_KEY,
  168. L"AppCaptureEnabled", &game_bar_enabled);
  169. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_DVR_POLICY_REG_KEY,
  170. L"AllowGameDVR", &game_dvr_allowed);
  171. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_DVR_REG_KEY,
  172. L"GameDVR_Enabled", &game_dvr_enabled);
  173. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_BAR_REG_KEY,
  174. L"HistoricalCaptureEnabled", &game_dvr_bg_recording);
  175. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_MODE_REG_KEY,
  176. L"AllowAutoGameMode", &game_mode_enabled);
  177. blog(LOG_INFO, "Windows 10 Gaming Features:");
  178. blog(LOG_INFO, "\tGame Bar: %s",
  179. (bool)game_bar_enabled.return_value ? "On" : "Off");
  180. blog(LOG_INFO, "\tGame DVR Allowed: %s",
  181. (bool)game_dvr_allowed.return_value ? "Yes" : "No");
  182. blog(LOG_INFO, "\tGame DVR: %s",
  183. (bool)game_dvr_enabled.return_value ? "On" : "Off");
  184. blog(LOG_INFO, "\tGame DVR Background Recording: %s",
  185. (bool)game_dvr_bg_recording.return_value ? "On" :
  186. "Off");
  187. blog(LOG_INFO, "\tGame Mode: %s",
  188. (bool)game_mode_enabled.return_value ? "On" : "Off");
  189. }
  190. void log_system_info(void)
  191. {
  192. struct win_version_info ver;
  193. get_win_ver(&ver);
  194. win_ver = (ver.major << 8) | ver.minor;
  195. log_processor_info();
  196. log_processor_cores();
  197. log_available_memory();
  198. log_windows_version();
  199. log_admin_status();
  200. log_aero();
  201. log_gaming_features();
  202. }
  203. struct obs_hotkeys_platform {
  204. int vk_codes[OBS_KEY_LAST_VALUE];
  205. };
  206. static int get_virtual_key(obs_key_t key)
  207. {
  208. switch (key) {
  209. case OBS_KEY_RETURN: return VK_RETURN;
  210. case OBS_KEY_ESCAPE: return VK_ESCAPE;
  211. case OBS_KEY_TAB: return VK_TAB;
  212. case OBS_KEY_BACKTAB: return VK_OEM_BACKTAB;
  213. case OBS_KEY_BACKSPACE: return VK_BACK;
  214. case OBS_KEY_INSERT: return VK_INSERT;
  215. case OBS_KEY_DELETE: return VK_DELETE;
  216. case OBS_KEY_PAUSE: return VK_PAUSE;
  217. case OBS_KEY_PRINT: return VK_SNAPSHOT;
  218. case OBS_KEY_CLEAR: return VK_CLEAR;
  219. case OBS_KEY_HOME: return VK_HOME;
  220. case OBS_KEY_END: return VK_END;
  221. case OBS_KEY_LEFT: return VK_LEFT;
  222. case OBS_KEY_UP: return VK_UP;
  223. case OBS_KEY_RIGHT: return VK_RIGHT;
  224. case OBS_KEY_DOWN: return VK_DOWN;
  225. case OBS_KEY_PAGEUP: return VK_PRIOR;
  226. case OBS_KEY_PAGEDOWN: return VK_NEXT;
  227. case OBS_KEY_SHIFT: return VK_SHIFT;
  228. case OBS_KEY_CONTROL: return VK_CONTROL;
  229. case OBS_KEY_ALT: return VK_MENU;
  230. case OBS_KEY_CAPSLOCK: return VK_CAPITAL;
  231. case OBS_KEY_NUMLOCK: return VK_NUMLOCK;
  232. case OBS_KEY_SCROLLLOCK: return VK_SCROLL;
  233. case OBS_KEY_F1: return VK_F1;
  234. case OBS_KEY_F2: return VK_F2;
  235. case OBS_KEY_F3: return VK_F3;
  236. case OBS_KEY_F4: return VK_F4;
  237. case OBS_KEY_F5: return VK_F5;
  238. case OBS_KEY_F6: return VK_F6;
  239. case OBS_KEY_F7: return VK_F7;
  240. case OBS_KEY_F8: return VK_F8;
  241. case OBS_KEY_F9: return VK_F9;
  242. case OBS_KEY_F10: return VK_F10;
  243. case OBS_KEY_F11: return VK_F11;
  244. case OBS_KEY_F12: return VK_F12;
  245. case OBS_KEY_F13: return VK_F13;
  246. case OBS_KEY_F14: return VK_F14;
  247. case OBS_KEY_F15: return VK_F15;
  248. case OBS_KEY_F16: return VK_F16;
  249. case OBS_KEY_F17: return VK_F17;
  250. case OBS_KEY_F18: return VK_F18;
  251. case OBS_KEY_F19: return VK_F19;
  252. case OBS_KEY_F20: return VK_F20;
  253. case OBS_KEY_F21: return VK_F21;
  254. case OBS_KEY_F22: return VK_F22;
  255. case OBS_KEY_F23: return VK_F23;
  256. case OBS_KEY_F24: return VK_F24;
  257. case OBS_KEY_SPACE: return VK_SPACE;
  258. case OBS_KEY_APOSTROPHE: return VK_OEM_7;
  259. case OBS_KEY_PLUS: return VK_OEM_PLUS;
  260. case OBS_KEY_COMMA: return VK_OEM_COMMA;
  261. case OBS_KEY_MINUS: return VK_OEM_MINUS;
  262. case OBS_KEY_PERIOD: return VK_OEM_PERIOD;
  263. case OBS_KEY_SLASH: return VK_OEM_2;
  264. case OBS_KEY_0: return '0';
  265. case OBS_KEY_1: return '1';
  266. case OBS_KEY_2: return '2';
  267. case OBS_KEY_3: return '3';
  268. case OBS_KEY_4: return '4';
  269. case OBS_KEY_5: return '5';
  270. case OBS_KEY_6: return '6';
  271. case OBS_KEY_7: return '7';
  272. case OBS_KEY_8: return '8';
  273. case OBS_KEY_9: return '9';
  274. case OBS_KEY_NUMASTERISK: return VK_MULTIPLY;
  275. case OBS_KEY_NUMPLUS: return VK_ADD;
  276. case OBS_KEY_NUMMINUS: return VK_SUBTRACT;
  277. case OBS_KEY_NUMPERIOD: return VK_DECIMAL;
  278. case OBS_KEY_NUMSLASH: return VK_DIVIDE;
  279. case OBS_KEY_NUM0: return VK_NUMPAD0;
  280. case OBS_KEY_NUM1: return VK_NUMPAD1;
  281. case OBS_KEY_NUM2: return VK_NUMPAD2;
  282. case OBS_KEY_NUM3: return VK_NUMPAD3;
  283. case OBS_KEY_NUM4: return VK_NUMPAD4;
  284. case OBS_KEY_NUM5: return VK_NUMPAD5;
  285. case OBS_KEY_NUM6: return VK_NUMPAD6;
  286. case OBS_KEY_NUM7: return VK_NUMPAD7;
  287. case OBS_KEY_NUM8: return VK_NUMPAD8;
  288. case OBS_KEY_NUM9: return VK_NUMPAD9;
  289. case OBS_KEY_SEMICOLON: return VK_OEM_1;
  290. case OBS_KEY_A: return 'A';
  291. case OBS_KEY_B: return 'B';
  292. case OBS_KEY_C: return 'C';
  293. case OBS_KEY_D: return 'D';
  294. case OBS_KEY_E: return 'E';
  295. case OBS_KEY_F: return 'F';
  296. case OBS_KEY_G: return 'G';
  297. case OBS_KEY_H: return 'H';
  298. case OBS_KEY_I: return 'I';
  299. case OBS_KEY_J: return 'J';
  300. case OBS_KEY_K: return 'K';
  301. case OBS_KEY_L: return 'L';
  302. case OBS_KEY_M: return 'M';
  303. case OBS_KEY_N: return 'N';
  304. case OBS_KEY_O: return 'O';
  305. case OBS_KEY_P: return 'P';
  306. case OBS_KEY_Q: return 'Q';
  307. case OBS_KEY_R: return 'R';
  308. case OBS_KEY_S: return 'S';
  309. case OBS_KEY_T: return 'T';
  310. case OBS_KEY_U: return 'U';
  311. case OBS_KEY_V: return 'V';
  312. case OBS_KEY_W: return 'W';
  313. case OBS_KEY_X: return 'X';
  314. case OBS_KEY_Y: return 'Y';
  315. case OBS_KEY_Z: return 'Z';
  316. case OBS_KEY_BRACKETLEFT: return VK_OEM_4;
  317. case OBS_KEY_BACKSLASH: return VK_OEM_5;
  318. case OBS_KEY_BRACKETRIGHT: return VK_OEM_6;
  319. case OBS_KEY_ASCIITILDE: return VK_OEM_3;
  320. case OBS_KEY_HENKAN: return VK_CONVERT;
  321. case OBS_KEY_MUHENKAN: return VK_NONCONVERT;
  322. case OBS_KEY_KANJI: return VK_KANJI;
  323. case OBS_KEY_TOUROKU: return VK_OEM_FJ_TOUROKU;
  324. case OBS_KEY_MASSYO: return VK_OEM_FJ_MASSHOU;
  325. case OBS_KEY_HANGUL: return VK_HANGUL;
  326. case OBS_KEY_BACKSLASH_RT102: return VK_OEM_102;
  327. case OBS_KEY_MOUSE1: return VK_LBUTTON;
  328. case OBS_KEY_MOUSE2: return VK_RBUTTON;
  329. case OBS_KEY_MOUSE3: return VK_MBUTTON;
  330. case OBS_KEY_MOUSE4: return VK_XBUTTON1;
  331. case OBS_KEY_MOUSE5: return VK_XBUTTON2;
  332. /* TODO: Implement keys for non-US keyboards */
  333. default:;
  334. }
  335. return 0;
  336. }
  337. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  338. {
  339. hotkeys->platform_context = bzalloc(sizeof(obs_hotkeys_platform_t));
  340. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++)
  341. hotkeys->platform_context->vk_codes[i] = get_virtual_key(i);
  342. return true;
  343. }
  344. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  345. {
  346. bfree(hotkeys->platform_context);
  347. hotkeys->platform_context = NULL;
  348. }
  349. static bool vk_down(DWORD vk)
  350. {
  351. short state = GetAsyncKeyState(vk);
  352. bool down = (state & 0x8000) != 0;
  353. return down;
  354. }
  355. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  356. obs_key_t key)
  357. {
  358. if (key == OBS_KEY_META) {
  359. return vk_down(VK_LWIN) || vk_down(VK_RWIN);
  360. }
  361. UNUSED_PARAMETER(context);
  362. return vk_down(obs_key_to_virtual_key(key));
  363. }
  364. void obs_key_to_str(obs_key_t key, struct dstr *str)
  365. {
  366. wchar_t name[128] = L"";
  367. UINT scan_code;
  368. int vk;
  369. if (key == OBS_KEY_NONE) {
  370. return;
  371. } else if (key >= OBS_KEY_MOUSE1 && key <= OBS_KEY_MOUSE29) {
  372. if (obs->hotkeys.translations[key]) {
  373. dstr_copy(str, obs->hotkeys.translations[key]);
  374. } else {
  375. dstr_printf(str, "Mouse %d",
  376. (int)(key - OBS_KEY_MOUSE1 + 1));
  377. }
  378. return;
  379. } if (key == OBS_KEY_PAUSE) {
  380. dstr_copy(str, obs_get_hotkey_translation(key, "Pause"));
  381. return;
  382. } else if (key == OBS_KEY_META) {
  383. dstr_copy(str, obs_get_hotkey_translation(key, "Windows"));
  384. return;
  385. }
  386. vk = obs_key_to_virtual_key(key);
  387. scan_code = MapVirtualKey(vk, 0) << 16;
  388. switch (vk) {
  389. case VK_HOME:
  390. case VK_END:
  391. case VK_LEFT:
  392. case VK_UP:
  393. case VK_RIGHT:
  394. case VK_DOWN:
  395. case VK_PRIOR:
  396. case VK_NEXT:
  397. case VK_INSERT:
  398. case VK_DELETE:
  399. case VK_NUMLOCK:
  400. scan_code |= 0x01000000;
  401. }
  402. if (scan_code != 0 && GetKeyNameTextW(scan_code, name, 128) != 0) {
  403. dstr_from_wcs(str, name);
  404. } else if (key != OBS_KEY_NONE) {
  405. dstr_copy(str, obs_key_to_name(key));
  406. }
  407. }
  408. obs_key_t obs_key_from_virtual_key(int code)
  409. {
  410. obs_hotkeys_platform_t *platform = obs->hotkeys.platform_context;
  411. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  412. if (platform->vk_codes[i] == code) {
  413. return (obs_key_t)i;
  414. }
  415. }
  416. return OBS_KEY_NONE;
  417. }
  418. int obs_key_to_virtual_key(obs_key_t key)
  419. {
  420. if (key == OBS_KEY_META)
  421. return VK_LWIN;
  422. return obs->hotkeys.platform_context->vk_codes[(int)key];
  423. }
  424. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  425. {
  426. struct dstr key_str = {0};
  427. obs_key_to_str(key, &key_str);
  428. if (!dstr_is_empty(&key_str)) {
  429. if (!dstr_is_empty(str)) {
  430. dstr_cat(str, " + ");
  431. }
  432. dstr_cat_dstr(str, &key_str);
  433. }
  434. dstr_free(&key_str);
  435. }
  436. void obs_key_combination_to_str(obs_key_combination_t combination,
  437. struct dstr *str)
  438. {
  439. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  440. add_combo_key(OBS_KEY_CONTROL, str);
  441. }
  442. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  443. add_combo_key(OBS_KEY_META, str);
  444. }
  445. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  446. add_combo_key(OBS_KEY_ALT, str);
  447. }
  448. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  449. add_combo_key(OBS_KEY_SHIFT, str);
  450. }
  451. if (combination.key != OBS_KEY_NONE) {
  452. add_combo_key(combination.key, str);
  453. }
  454. }
  455. bool sym_initialize_called = false;
  456. void reset_win32_symbol_paths(void)
  457. {
  458. static BOOL (WINAPI *sym_initialize_w)(HANDLE, const wchar_t*, BOOL);
  459. static BOOL (WINAPI *sym_set_search_path_w)(HANDLE, const wchar_t*);
  460. static bool funcs_initialized = false;
  461. static bool initialize_success = false;
  462. struct obs_module *module = obs->first_module;
  463. struct dstr path_str = {0};
  464. DARRAY(char*) paths;
  465. wchar_t *path_str_w = NULL;
  466. char *abspath;
  467. da_init(paths);
  468. if (!funcs_initialized) {
  469. HMODULE mod;
  470. funcs_initialized = true;
  471. mod = LoadLibraryW(L"DbgHelp");
  472. if (!mod)
  473. return;
  474. sym_initialize_w = (void*)GetProcAddress(mod, "SymInitializeW");
  475. sym_set_search_path_w = (void*)GetProcAddress(mod,
  476. "SymSetSearchPathW");
  477. if (!sym_initialize_w || !sym_set_search_path_w)
  478. return;
  479. initialize_success = true;
  480. }
  481. if (!initialize_success)
  482. return;
  483. abspath = os_get_abs_path_ptr(".");
  484. if (abspath)
  485. da_push_back(paths, &abspath);
  486. while (module) {
  487. bool found = false;
  488. struct dstr path = {0};
  489. char *path_end;
  490. dstr_copy(&path, module->bin_path);
  491. dstr_replace(&path, "/", "\\");
  492. path_end = strrchr(path.array, '\\');
  493. if (!path_end) {
  494. module = module->next;
  495. dstr_free(&path);
  496. continue;
  497. }
  498. *path_end = 0;
  499. for (size_t i = 0; i < paths.num; i++) {
  500. const char *existing_path = paths.array[i];
  501. if (astrcmpi(path.array, existing_path) == 0) {
  502. found = true;
  503. break;
  504. }
  505. }
  506. if (!found) {
  507. abspath = os_get_abs_path_ptr(path.array);
  508. if (abspath)
  509. da_push_back(paths, &abspath);
  510. }
  511. dstr_free(&path);
  512. module = module->next;
  513. }
  514. for (size_t i = 0; i < paths.num; i++) {
  515. const char *path = paths.array[i];
  516. if (path && *path) {
  517. if (i != 0)
  518. dstr_cat(&path_str, ";");
  519. dstr_cat(&path_str, paths.array[i]);
  520. }
  521. }
  522. if (path_str.array) {
  523. os_utf8_to_wcs_ptr(path_str.array, path_str.len, &path_str_w);
  524. if (path_str_w) {
  525. if (!sym_initialize_called) {
  526. sym_initialize_w(GetCurrentProcess(),
  527. path_str_w, false);
  528. sym_initialize_called = true;
  529. } else {
  530. sym_set_search_path_w(GetCurrentProcess(),
  531. path_str_w);
  532. }
  533. bfree(path_str_w);
  534. }
  535. }
  536. for (size_t i = 0; i < paths.num; i++)
  537. bfree(paths.array[i]);
  538. dstr_free(&path_str);
  539. da_free(paths);
  540. }
  541. void initialize_com(void)
  542. {
  543. CoInitializeEx(0, COINIT_MULTITHREADED);
  544. }
  545. void uninitialize_com(void)
  546. {
  547. CoUninitialize();
  548. }