forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show error dialog for missing runtime/framework (dotnet/core-setup#8509)
Commit migrated from dotnet/core-setup@346b717
- Loading branch information
1 parent
4d82434
commit c17bb03
Showing
11 changed files
with
372 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#include "apphost.windows.h" | ||
#include "error_codes.h" | ||
#include "pal.h" | ||
#include "trace.h" | ||
#include "utils.h" | ||
|
||
namespace | ||
{ | ||
pal::string_t g_buffered_errors; | ||
|
||
void buffering_trace_writer(const pal::char_t* message) | ||
{ | ||
// Add to buffer for later use. | ||
g_buffered_errors.append(message).append(_X("\n")); | ||
// Also write to stderr immediately | ||
pal::err_fputs(message); | ||
} | ||
|
||
// Determines if the current module (apphost executable) is marked as a Windows GUI application | ||
bool is_gui_application() | ||
{ | ||
HMODULE module = ::GetModuleHandleW(nullptr); | ||
assert(module != nullptr); | ||
|
||
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format | ||
BYTE *bytes = reinterpret_cast<BYTE *>(module); | ||
UINT32 pe_header_offset = reinterpret_cast<IMAGE_DOS_HEADER *>(bytes)->e_lfanew; | ||
UINT16 subsystem = reinterpret_cast<IMAGE_NT_HEADERS *>(bytes + pe_header_offset)->OptionalHeader.Subsystem; | ||
|
||
return subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI; | ||
} | ||
|
||
void write_errors_to_event_log(const pal::char_t *executable_path, const pal::char_t *executable_name) | ||
{ | ||
// Report errors to the Windows Event Log. | ||
auto eventSource = ::RegisterEventSourceW(nullptr, _X(".NET Runtime")); | ||
const DWORD traceErrorID = 1023; // Matches CoreCLR ERT_UnmanagedFailFast | ||
pal::string_t message; | ||
message.append(_X("Description: A .NET Core application failed.\n")); | ||
message.append(_X("Application: ")).append(executable_name).append(_X("\n")); | ||
message.append(_X("Path: ")).append(executable_path).append(_X("\n")); | ||
message.append(_X("Message: ")).append(g_buffered_errors).append(_X("\n")); | ||
|
||
LPCWSTR messages[] = {message.c_str()}; | ||
::ReportEventW(eventSource, EVENTLOG_ERROR_TYPE, 0, traceErrorID, nullptr, 1, 0, messages, nullptr); | ||
::DeregisterEventSource(eventSource); | ||
} | ||
|
||
void show_error_dialog(const pal::char_t *executable_name, int error_code) | ||
{ | ||
// Show message dialog for UI apps with actionable errors | ||
if (error_code != StatusCode::CoreHostLibMissingFailure // missing hostfxr | ||
&& error_code != StatusCode::FrameworkMissingFailure) // missing framework | ||
return; | ||
|
||
pal::string_t gui_errors_disabled; | ||
if (pal::getenv(_X("DOTNET_DISABLE_GUI_ERRORS"), &gui_errors_disabled) && pal::xtoi(gui_errors_disabled.c_str()) == 1) | ||
return; | ||
|
||
pal::string_t dialogMsg = _X("To run this application, you must install .NET Core.\n\n"); | ||
pal::string_t url; | ||
if (error_code == StatusCode::CoreHostLibMissingFailure) | ||
{ | ||
url = get_download_url(); | ||
} | ||
else if (error_code == StatusCode::FrameworkMissingFailure) | ||
{ | ||
// We don't have a great way of passing out different kinds of detailed error info across components, so | ||
// just match the expected error string. See fx_resolver.messages.cpp. | ||
pal::string_t line; | ||
pal::stringstream_t ss(g_buffered_errors); | ||
while (std::getline(ss, line, _X('\n'))){ | ||
const pal::string_t prefix = _X("The framework '"); | ||
const pal::string_t suffix = _X("' was not found."); | ||
const pal::string_t url_prefix = _X(" - ") DOTNET_CORE_APPLAUNCH_URL _X("?"); | ||
if (starts_with(line, prefix, true) && ends_with(line, suffix, true)) | ||
{ | ||
dialogMsg.append(line); | ||
dialogMsg.append(_X("\n\n")); | ||
} | ||
else if (starts_with(line, url_prefix, true)) | ||
{ | ||
size_t offset = url_prefix.length() - pal::strlen(DOTNET_CORE_APPLAUNCH_URL) - 1; | ||
url = line.substr(offset, line.length() - offset); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
dialogMsg.append(_X("Would you like to download it now?")); | ||
|
||
assert(url.length() > 0); | ||
url.append(_X("&apphost_version=")); | ||
url.append(_STRINGIFY(COMMON_HOST_PKG_VER)); | ||
|
||
trace::verbose(_X("Showing error dialog for application: '%s' - error code: 0x%x - url: '%s'"), executable_name, error_code, url.c_str()); | ||
if (::MessageBoxW(nullptr, dialogMsg.c_str(), executable_name, MB_ICONERROR | MB_YESNO) == IDYES) | ||
{ | ||
// Open the URL in default browser | ||
::ShellExecuteW( | ||
nullptr, | ||
_X("open"), | ||
url.c_str(), | ||
nullptr, | ||
nullptr, | ||
SW_SHOWNORMAL); | ||
} | ||
} | ||
} | ||
|
||
void apphost::buffer_errors() | ||
{ | ||
trace::verbose(_X("Redirecting errors to custom writer.")); | ||
trace::set_error_writer(buffering_trace_writer); | ||
} | ||
|
||
void apphost::write_buffered_errors(int error_code) | ||
{ | ||
if (g_buffered_errors.empty()) | ||
return; | ||
|
||
pal::string_t executable_path; | ||
pal::string_t executable_name; | ||
if (pal::get_own_executable_path(&executable_path)) | ||
{ | ||
executable_name = get_filename(executable_path); | ||
} | ||
|
||
write_errors_to_event_log(executable_path.c_str(), executable_name.c_str()); | ||
|
||
if (is_gui_application()) | ||
show_error_dialog(executable_name.c_str(), error_code); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#ifndef __APPHOST_WINDOWS_H__ | ||
#define __APPHOST_WINDOWS_H__ | ||
|
||
namespace apphost | ||
{ | ||
void buffer_errors(); | ||
void write_buffered_errors(int error_code); | ||
} | ||
|
||
#endif // __APPHOST_WINDOWS_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.