HMODULE LoadNativeHost(LPCTSTR szHostModuleName) { char localPath[PATH_MAX]; GetNativeBootstrapperDirectory(localPath); strcat(localPath, "/"); strcat(localPath, szHostModuleName); return dlopen(localPath, RTLD_NOW | RTLD_GLOBAL); }
int CallApplicationProcessMain(int argc, dnx::char_t* argv[], dnx::trace_writer& trace_writer) { const auto currentDirectory = GetNativeBootstrapperDirectory(); CALL_APPLICATION_MAIN_DATA data = { 0 }; data.argc = argc; data.argv = const_cast<const dnx::char_t**>(argv); data.runtimeDirectory = currentDirectory.c_str(); dnx::char_t appBaseBuffer[MAX_PATH]; if (!GetApplicationBase(currentDirectory, argc, argv, appBaseBuffer)) { return 1; } data.applicationBase = appBaseBuffer; try { const dnx::char_t* hostModuleName = #if defined(CORECLR_WIN) #if defined(ONECORE) || defined(ARM) _X("dnx.onecore.coreclr.dll"); #else _X("dnx.win32.coreclr.dll"); #endif #elif defined(CORECLR_DARWIN) _X("dnx.coreclr.dylib"); #elif defined(CORECLR_LINUX) _X("dnx.coreclr.so"); #else _X("dnx.clr.dll"); #endif // Note: need to keep as ASCII as GetProcAddress function takes ASCII params return CallApplicationMain(hostModuleName, "CallApplicationMain", &data, trace_writer); } catch (const std::exception& ex) { xout << dnx::utils::to_xstring_t(ex.what()) << std::endl; return 1; } }
int CallApplicationMain(const char* moduleName, const char* functionName, CALL_APPLICATION_MAIN_DATA* data, dnx::trace_writer& trace_writer) { auto localPath = GetNativeBootstrapperDirectory().append("/").append(moduleName); void* host = nullptr; try { host = dlopen(localPath.c_str(), RTLD_NOW | RTLD_GLOBAL); if (!host) { throw std::runtime_error(std::string("Failed to load: ").append(moduleName)); } trace_writer.write(std::string("Loaded module: ").append(moduleName), true); auto pfnCallApplicationMain = reinterpret_cast<FnCallApplicationMain>(dlsym(host, functionName)); if (!pfnCallApplicationMain) { std::ostringstream oss; oss << "Failed to find export '" << functionName << "' in " << moduleName; throw std::runtime_error(oss.str()); } trace_writer.write(std::string("Found export: ").append(functionName), true); auto result = pfnCallApplicationMain(data); dlclose(host); return result == 0 ? data->exitcode : result; } catch(const std::exception& ex) { if(host) { dlclose(host); } throw; } }