diff --git a/src/corehost/cli/hostpolicy.cpp b/src/corehost/cli/hostpolicy.cpp index d69ccae70..324eba54a 100644 --- a/src/corehost/cli/hostpolicy.cpp +++ b/src/corehost/cli/hostpolicy.cpp @@ -67,39 +67,40 @@ int run(const arguments_t& args) "FX_DEPS_FILE" }; - auto tpa_paths_cstr = pal::to_stdstring(probe_paths.tpa); - auto app_base_cstr = pal::to_stdstring(args.app_dir); - auto native_dirs_cstr = pal::to_stdstring(probe_paths.native); - auto resources_dirs_cstr = pal::to_stdstring(probe_paths.resources); + std::vector tpa_paths_cstr, app_base_cstr, native_dirs_cstr, resources_dirs_cstr, fx_deps, deps; + pal::to_clrstring(probe_paths.tpa, &tpa_paths_cstr); + pal::to_clrstring(args.app_dir, &app_base_cstr); + pal::to_clrstring(probe_paths.native, &native_dirs_cstr); + pal::to_clrstring(probe_paths.resources, &resources_dirs_cstr); - std::string fx_deps = pal::to_stdstring(resolver.get_fx_deps_file()); - std::string deps = pal::to_stdstring(resolver.get_deps_file() + _X(";")) + fx_deps; + pal::to_clrstring(resolver.get_fx_deps_file(), &fx_deps); + pal::to_clrstring(resolver.get_deps_file() + _X(";") + resolver.get_fx_deps_file(), &deps); std::vector property_values = { // TRUSTED_PLATFORM_ASSEMBLIES - tpa_paths_cstr.c_str(), + tpa_paths_cstr.data(), // APP_PATHS - app_base_cstr.c_str(), + app_base_cstr.data(), // APP_NI_PATHS - app_base_cstr.c_str(), + app_base_cstr.data(), // NATIVE_DLL_SEARCH_DIRECTORIES - native_dirs_cstr.c_str(), + native_dirs_cstr.data(), // PLATFORM_RESOURCE_ROOTS - resources_dirs_cstr.c_str(), + resources_dirs_cstr.data(), // AppDomainCompatSwitch "UseLatestBehaviorWhenTFMNotSpecified", // APP_CONTEXT_BASE_DIRECTORY - app_base_cstr.c_str(), + app_base_cstr.data(), // APP_CONTEXT_DEPS_FILES, - deps.c_str(), + deps.data(), // FX_DEPS_FILE - fx_deps.c_str() + fx_deps.data() }; for (int i = 0; i < g_init.cfg_keys.size(); ++i) { - property_keys.push_back(g_init.cfg_keys[i].c_str()); - property_values.push_back(g_init.cfg_values[i].c_str()); + property_keys.push_back(g_init.cfg_keys[i].data()); + property_values.push_back(g_init.cfg_values[i].data()); } size_t property_size = property_keys.size(); @@ -121,20 +122,20 @@ int run(const arguments_t& args) for (size_t i = 0; i < property_size; ++i) { pal::string_t key, val; - pal::to_palstring(property_keys[i], &key); - pal::to_palstring(property_values[i], &val); + pal::clr_palstring(property_keys[i], &key); + pal::clr_palstring(property_values[i], &val); trace::verbose(_X("Property %s = %s"), key.c_str(), val.c_str()); } } - std::string own_path; - pal::to_stdstring(args.own_path.c_str(), &own_path); + std::vector own_path; + pal::to_clrstring(args.own_path, &own_path); // Initialize CoreCLR coreclr::host_handle_t host_handle; coreclr::domain_id_t domain_id; auto hr = coreclr::initialize( - own_path.c_str(), + own_path.data(), "clrhost", property_keys.data(), property_values.data(), @@ -147,28 +148,31 @@ int run(const arguments_t& args) return StatusCode::CoreClrInitFailure; } - if (trace::is_enabled()) - { - pal::string_t arg_str; - for (int i = 0; i < args.app_argc; i++) - { - arg_str.append(args.app_argv[i]); - arg_str.append(_X(",")); - } - trace::info(_X("Launch host: %s app: %s, argc: %d args: %s"), args.own_path.c_str(), - args.managed_application.c_str(), args.app_argc, arg_str.c_str()); - } - - // Initialize with empty strings - std::vector argv_strs(args.app_argc); + // Initialize clr strings for arguments + std::vector> argv_strs(args.app_argc); std::vector argv(args.app_argc); for (int i = 0; i < args.app_argc; i++) { - pal::to_stdstring(args.app_argv[i], &argv_strs[i]); - argv[i] = argv_strs[i].c_str(); + pal::to_clrstring(args.app_argv[i], &argv_strs[i]); + argv[i] = argv_strs[i].data(); } - std::string managed_app = pal::to_stdstring(args.managed_application); + if (trace::is_enabled()) + { + pal::string_t arg_str; + for (int i = 0; i < argv.size(); i++) + { + pal::string_t cur; + pal::clr_palstring(argv[i], &cur); + arg_str.append(cur); + arg_str.append(_X(",")); + } + trace::info(_X("Launch host: %s, app: %s, argc: %d, args: %s"), args.own_path.c_str(), + args.managed_application.c_str(), args.app_argc, arg_str.c_str()); + } + + std::vector managed_app; + pal::to_clrstring(args.managed_application, &managed_app); // Leave breadcrumbs for servicing. breadcrumb_writer_t writer(&breadcrumbs); @@ -181,7 +185,7 @@ int run(const arguments_t& args) domain_id, argv.size(), argv.data(), - managed_app.c_str(), + managed_app.data(), &exit_code); if (!SUCCEEDED(hr)) diff --git a/src/corehost/cli/libhost.h b/src/corehost/cli/libhost.h index ea2db1899..9bac26103 100644 --- a/src/corehost/cli/libhost.h +++ b/src/corehost/cli/libhost.h @@ -170,8 +170,8 @@ private: struct hostpolicy_init_t { - std::vector cfg_keys; - std::vector cfg_values; + std::vector> cfg_keys; + std::vector> cfg_values; pal::string_t deps_file; std::vector probe_paths; pal::string_t fx_dir; @@ -197,8 +197,8 @@ struct hostpolicy_init_t } trace::verbose(_X("Reading from host interface version: [0x%04x:%d] to initialize policy version: [0x%04x:%d]"), input->version_hi, input->version_lo, HOST_INTERFACE_LAYOUT_VERSION_HI, HOST_INTERFACE_LAYOUT_VERSION_LO); - make_stdstr_arr(input->config_keys.len, input->config_keys.arr, &init->cfg_keys); - make_stdstr_arr(input->config_values.len, input->config_values.arr, &init->cfg_values); + make_clrstr_arr(input->config_keys.len, input->config_keys.arr, &init->cfg_keys); + make_clrstr_arr(input->config_values.len, input->config_values.arr, &init->cfg_values); init->fx_dir = input->fx_dir; init->fx_name = input->fx_name; @@ -224,12 +224,12 @@ private: } } - static void make_stdstr_arr(int argc, const pal::char_t** argv, std::vector* out) + static void make_clrstr_arr(int argc, const pal::char_t** argv, std::vector>* out) { - out->reserve(argc); + out->resize(argc); for (int i = 0; i < argc; ++i) { - out->push_back(pal::to_stdstring(argv[i])); + pal::to_clrstring(pal::string_t(argv[i]), &(*out)[i]); } } }; diff --git a/src/corehost/common/pal.h b/src/corehost/common/pal.h index 92fa009aa..86adf0c18 100644 --- a/src/corehost/common/pal.h +++ b/src/corehost/common/pal.h @@ -118,9 +118,9 @@ namespace pal inline void err_vprintf(const char_t* format, va_list vl) { ::vfwprintf(stderr, format, vl); ::fputws(_X("\r\n"), stderr); } pal::string_t to_palstring(const std::string& str); - std::string to_stdstring(const pal::string_t& str); void to_palstring(const char* str, pal::string_t* out); - void to_stdstring(const pal::char_t* str, std::string* out); + void to_clrstring(const pal::string_t& str, std::vector* out); + void clr_palstring(const char* out, pal::string_t* str); #else #ifdef COREHOST_MAKE_DLL #define SHARED_API extern "C" @@ -160,9 +160,9 @@ namespace pal inline size_t strlen(const char_t* str) { return ::strlen(str); } inline void err_vprintf(const char_t* format, va_list vl) { ::vfprintf(stderr, format, vl); ::fputc('\n', stderr); } inline pal::string_t to_palstring(const std::string& str) { return str; } - inline std::string to_stdstring(const pal::string_t& str) { return str; } inline void to_palstring(const char* str, pal::string_t* out) { out->assign(str); } - inline void to_stdstring(const char_t* str, std::string* out) { out->assign(str); } + inline void to_clrstring(const pal::string_t& str, std::vector* out) { out->assign(str.begin(), str.end()); out->push_back('\0'); } + inline void clr_palstring(const char* clr, pal::string_t* str) { str->assign(clr); } #endif bool touch_file(const pal::string_t& path); diff --git a/src/corehost/common/pal.windows.cpp b/src/corehost/common/pal.windows.cpp index 73f9b597a..f7c608d21 100644 --- a/src/corehost/common/pal.windows.cpp +++ b/src/corehost/common/pal.windows.cpp @@ -253,11 +253,6 @@ bool pal::get_own_executable_path(string_t* recv) return true; } -std::string pal::to_stdstring(const string_t& str) -{ - return t_converter.to_bytes(str); -} - pal::string_t pal::to_palstring(const std::string& str) { return t_converter.from_bytes(str); @@ -268,9 +263,29 @@ void pal::to_palstring(const char* str, pal::string_t* out) out->assign(t_converter.from_bytes(str)); } -void pal::to_stdstring(const pal::char_t* str, std::string* out) +void pal::to_clrstring(const pal::string_t& str, std::vector* out) { - out->assign(t_converter.to_bytes(str)); + // Pass -1 as we want explicit null termination in the char buffer. + size_t size = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, nullptr, 0, nullptr, nullptr); + out->resize(size, '\0'); + if (size == 0) + { + return; + } + ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, out->data(), out->size(), nullptr, nullptr); +} + +void pal::clr_palstring(const char* out, pal::string_t* str) +{ + // No need of explicit null termination, so pass in the actual length. + int len = ::strlen(out); + size_t size = ::MultiByteToWideChar(CP_ACP, 0, out, len, nullptr, 0); + str->resize(size, '\0'); + if (size == 0) + { + return; + } + ::MultiByteToWideChar(CP_ACP, 0, out, len, &(*str)[0], str->size()); } bool pal::realpath(string_t* path)