From 1b5a331c5604d469517fa3f18e6000020dbf31c6 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 5 Oct 2020 03:10:05 -0700 Subject: [PATCH] coreaudio-encoder: Refactor windows import Refactors the windows import library section so that it uses a list, and remove a slightly unnecessary macro in release_lib --- plugins/coreaudio-encoder/windows-imports.h | 76 ++++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/plugins/coreaudio-encoder/windows-imports.h b/plugins/coreaudio-encoder/windows-imports.h index a78eb5043..a79148e28 100644 --- a/plugins/coreaudio-encoder/windows-imports.h +++ b/plugins/coreaudio-encoder/windows-imports.h @@ -361,49 +361,57 @@ static HMODULE audio_toolbox = NULL; static void release_lib(void) { -#define RELEASE_LIB(x) \ - if (x) { \ - FreeLibrary(x); \ - x = NULL; \ + if (audio_toolbox) { + FreeLibrary(audio_toolbox); + audio_toolbox = NULL; + } +} + +static bool load_from_shell_path(REFKNOWNFOLDERID rfid, const wchar_t *subpath) +{ + wchar_t *sh_path; + if (SHGetKnownFolderPath(rfid, 0, NULL, &sh_path) != S_OK) { + CA_LOG(LOG_WARNING, "Could not retrieve shell path"); + return false; } - RELEASE_LIB(audio_toolbox); -#undef RELEASE_LIB + wchar_t path[MAX_PATH]; + _snwprintf(path, MAX_PATH, L"%s\\%s\\%s", sh_path, subpath, + L"CoreAudioToolbox.dll"); + CoTaskMemFree(sh_path); + + audio_toolbox = LoadLibraryW(path); + return !!audio_toolbox; } static bool load_lib(void) { - PWSTR common_path; - if (SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon, 0, NULL, - &common_path) != S_OK) { - CA_LOG(LOG_WARNING, "Could not retrieve common files path"); - return false; - } + /* -------------------------------------------- */ + /* attempt to load from path */ - struct dstr path = {0}; - dstr_printf(&path, "%S\\Apple\\Apple Application Support", common_path); - CoTaskMemFree(common_path); - - wchar_t *w_path = dstr_to_wcs(&path); - dstr_free(&path); - - SetDllDirectory(w_path); - bfree(w_path); - -#define LOAD_LIB(x, n) \ - x = LoadLibrary(TEXT(n)); \ - if (!x) \ - CA_LOG(LOG_DEBUG, "Failed loading library '" n "'"); - - LOAD_LIB(audio_toolbox, "CoreAudioToolbox.dll"); -#undef LOAD_LIB - - SetDllDirectory(NULL); - - if (audio_toolbox) + audio_toolbox = LoadLibraryW(L"CoreAudioToolbox.dll"); + if (!!audio_toolbox) return true; - release_lib(); + /* -------------------------------------------- */ + /* attempt to load from known install locations */ + + struct path_list_t { + REFKNOWNFOLDERID rfid; + const wchar_t *subpath; + }; + + path_list_t path_list[] = { + {FOLDERID_ProgramFilesCommon, + L"Apple\\Apple Application Support"}, + }; + + for (auto &val : path_list) { + if (load_from_shell_path(val.rfid, val.subpath)) { + return true; + } + } + return false; }