-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Format WinINet HRESULT messages #6321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
starSumi
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
starSumi:fix/6319-wininet-hresult-message
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #include "pch.h" | ||
| #include <wininet.h> | ||
| #include "Public/AppInstallerErrors.h" | ||
| #include "Public/AppInstallerLogging.h" | ||
| #include "Public/AppInstallerStrings.h" | ||
|
|
@@ -343,6 +344,55 @@ namespace AppInstaller | |
| UnknownHResultInformation(hr).GetDescription(); | ||
| } | ||
|
|
||
| int GetSystemErrorCode(HRESULT hr) | ||
| { | ||
| return static_cast<int>(HRESULT_FACILITY(hr) == FACILITY_WIN32 ? HRESULT_CODE(hr) : hr); | ||
| } | ||
|
|
||
| // WinINet errors are not present in the system message table. | ||
| std::optional<std::string> GetWinInetErrorMessage(int errorCode) | ||
| { | ||
| if (errorCode < ERROR_INTERNET_OUT_OF_HANDLES || errorCode > INTERNET_ERROR_LAST) | ||
| { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| wil::unique_hmodule module{ LoadLibraryExW(L"wininet.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) }; | ||
| if (!module) | ||
| { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| LPWSTR buffer = nullptr; | ||
| const DWORD messageLength = FormatMessageW( | ||
| FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, | ||
| module.get(), errorCode, 0, reinterpret_cast<LPWSTR>(&buffer), 0, nullptr); | ||
| if (!messageLength) | ||
| { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| auto freeBuffer = wil::scope_exit([&]() { LocalFree(buffer); }); | ||
| std::string message = Utility::ConvertToUTF8(std::wstring_view{ buffer, messageLength }); | ||
| Utility::Trim(message); | ||
| return message.empty() ? std::nullopt : std::optional<std::string>{ std::move(message) }; | ||
| } | ||
|
|
||
| std::string GetSystemErrorMessage(HRESULT hr) | ||
| { | ||
| const int errorCode = GetSystemErrorCode(hr); | ||
| if (HRESULT_FACILITY(hr) == FACILITY_WIN32) | ||
| { | ||
| auto winInetMessage = GetWinInetErrorMessage(errorCode); | ||
| if (winInetMessage) | ||
| { | ||
| return std::move(winInetMessage).value(); | ||
| } | ||
| } | ||
|
|
||
| return std::system_category().message(errorCode); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you intentionally change the Win32 errors to look up via code only? I don't know that it is required, and it would make the code above cleaner to simply extract the code when needed rather than unconditionally. |
||
| } | ||
|
|
||
| void GetUserPresentableMessageForHR(std::ostringstream& strstr, HRESULT hr) | ||
| { | ||
| strstr << "0x" << Logging::SetHRFormat << hr << " : "; | ||
|
|
@@ -361,7 +411,7 @@ namespace AppInstaller | |
| } | ||
| else | ||
| { | ||
| strstr << std::system_category().message(hr); | ||
| strstr << GetSystemErrorMessage(hr); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -437,7 +487,7 @@ namespace AppInstaller | |
| } | ||
| else | ||
| { | ||
| return Utility::LocIndString{ std::system_category().message(m_value) }; | ||
| return Utility::LocIndString{ GetSystemErrorMessage(m_value) }; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should specify
LOAD_LIBRARY_AS_DATAFILEsince we are only extracting a message.