11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include "uv.h" 231cb0ef41Sopenharmony_ci#include "internal.h" 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_cistatic int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciint uv_dlopen(const char* filename, uv_lib_t* lib) { 291cb0ef41Sopenharmony_ci WCHAR filename_w[32768]; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci lib->handle = NULL; 321cb0ef41Sopenharmony_ci lib->errmsg = NULL; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci if (!MultiByteToWideChar(CP_UTF8, 351cb0ef41Sopenharmony_ci 0, 361cb0ef41Sopenharmony_ci filename, 371cb0ef41Sopenharmony_ci -1, 381cb0ef41Sopenharmony_ci filename_w, 391cb0ef41Sopenharmony_ci ARRAY_SIZE(filename_w))) { 401cb0ef41Sopenharmony_ci return uv__dlerror(lib, filename, GetLastError()); 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci lib->handle = LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 441cb0ef41Sopenharmony_ci if (lib->handle == NULL) { 451cb0ef41Sopenharmony_ci return uv__dlerror(lib, filename, GetLastError()); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci return 0; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_civoid uv_dlclose(uv_lib_t* lib) { 531cb0ef41Sopenharmony_ci if (lib->errmsg) { 541cb0ef41Sopenharmony_ci LocalFree((void*)lib->errmsg); 551cb0ef41Sopenharmony_ci lib->errmsg = NULL; 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci if (lib->handle) { 591cb0ef41Sopenharmony_ci /* Ignore errors. No good way to signal them without leaking memory. */ 601cb0ef41Sopenharmony_ci FreeLibrary(lib->handle); 611cb0ef41Sopenharmony_ci lib->handle = NULL; 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci} 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciint uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) { 671cb0ef41Sopenharmony_ci /* Cast though integer to suppress pedantic warning about forbidden cast. */ 681cb0ef41Sopenharmony_ci *ptr = (void*)(uintptr_t) GetProcAddress(lib->handle, name); 691cb0ef41Sopenharmony_ci return uv__dlerror(lib, "", *ptr ? 0 : GetLastError()); 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciconst char* uv_dlerror(const uv_lib_t* lib) { 741cb0ef41Sopenharmony_ci return lib->errmsg ? lib->errmsg : "no error"; 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cistatic void uv__format_fallback_error(uv_lib_t* lib, int errorno){ 791cb0ef41Sopenharmony_ci static const CHAR fallback_error[] = "error: %1!d!"; 801cb0ef41Sopenharmony_ci DWORD_PTR args[1]; 811cb0ef41Sopenharmony_ci args[0] = (DWORD_PTR) errorno; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 841cb0ef41Sopenharmony_ci FORMAT_MESSAGE_ARGUMENT_ARRAY | 851cb0ef41Sopenharmony_ci FORMAT_MESSAGE_ALLOCATE_BUFFER, 861cb0ef41Sopenharmony_ci fallback_error, 0, 0, 871cb0ef41Sopenharmony_ci (LPSTR) &lib->errmsg, 881cb0ef41Sopenharmony_ci 0, (va_list*) args); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_cistatic int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno) { 941cb0ef41Sopenharmony_ci DWORD_PTR arg; 951cb0ef41Sopenharmony_ci DWORD res; 961cb0ef41Sopenharmony_ci char* msg; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci if (lib->errmsg) { 991cb0ef41Sopenharmony_ci LocalFree(lib->errmsg); 1001cb0ef41Sopenharmony_ci lib->errmsg = NULL; 1011cb0ef41Sopenharmony_ci } 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci if (errorno == 0) 1041cb0ef41Sopenharmony_ci return 0; 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | 1071cb0ef41Sopenharmony_ci FORMAT_MESSAGE_FROM_SYSTEM | 1081cb0ef41Sopenharmony_ci FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno, 1091cb0ef41Sopenharmony_ci MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 1101cb0ef41Sopenharmony_ci (LPSTR) &lib->errmsg, 0, NULL); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci if (!res && (GetLastError() == ERROR_MUI_FILE_NOT_FOUND || 1131cb0ef41Sopenharmony_ci GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND)) { 1141cb0ef41Sopenharmony_ci res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | 1151cb0ef41Sopenharmony_ci FORMAT_MESSAGE_FROM_SYSTEM | 1161cb0ef41Sopenharmony_ci FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno, 1171cb0ef41Sopenharmony_ci 0, (LPSTR) &lib->errmsg, 0, NULL); 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci if (res && errorno == ERROR_BAD_EXE_FORMAT && strstr(lib->errmsg, "%1")) { 1211cb0ef41Sopenharmony_ci msg = lib->errmsg; 1221cb0ef41Sopenharmony_ci lib->errmsg = NULL; 1231cb0ef41Sopenharmony_ci arg = (DWORD_PTR) filename; 1241cb0ef41Sopenharmony_ci res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | 1251cb0ef41Sopenharmony_ci FORMAT_MESSAGE_ARGUMENT_ARRAY | 1261cb0ef41Sopenharmony_ci FORMAT_MESSAGE_FROM_STRING, 1271cb0ef41Sopenharmony_ci msg, 1281cb0ef41Sopenharmony_ci 0, 0, (LPSTR) &lib->errmsg, 0, (va_list*) &arg); 1291cb0ef41Sopenharmony_ci LocalFree(msg); 1301cb0ef41Sopenharmony_ci } 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci if (!res) 1331cb0ef41Sopenharmony_ci uv__format_fallback_error(lib, errorno); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci return -1; 1361cb0ef41Sopenharmony_ci} 137