Add in verbose error message and telemetry for SetWindowsHookEx failure (#6454)

* Updated error message when SetWindowsHookEx fails to show correct error message

* Added telemetry for exception in SG, FZ and KBM

* Rename exception to error
This commit is contained in:
Arjun Balgovind
2020-09-09 14:27:40 -07:00
committed by GitHub
parent f61e9d389f
commit fb1888f01f
14 changed files with 90 additions and 45 deletions

View File

@@ -11,12 +11,6 @@
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "shlwapi.lib")
namespace localized_strings
{
const wchar_t LAST_ERROR_FORMAT_STRING[] = L"%s failed with error %d: %s";
const wchar_t LAST_ERROR_TITLE_STRING[] = L"Error";
}
std::optional<RECT> get_window_pos(HWND hwnd)
{
RECT window;
@@ -91,7 +85,7 @@ std::optional<std::wstring> get_last_error_message(const DWORD dw)
return message;
}
void show_last_error_message(LPCWSTR lpszFunction, DWORD dw)
void show_last_error_message(LPCWSTR lpszFunction, DWORD dw, LPCWSTR errorTitle)
{
const auto system_message = get_last_error_message(dw);
if (!system_message.has_value())
@@ -107,7 +101,7 @@ void show_last_error_message(LPCWSTR lpszFunction, DWORD dw)
lpszFunction,
dw,
system_message->c_str());
MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, localized_strings::LAST_ERROR_TITLE_STRING, MB_OK);
MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, errorTitle, MB_OK | MB_ICONERROR);
LocalFree(lpDisplayBuf);
}
}

View File

@@ -6,6 +6,13 @@
#include <memory>
#include <vector>
namespace localized_strings
{
const wchar_t LAST_ERROR_FORMAT_STRING[] = L"%s failed with error %d: %s";
const wchar_t LAST_ERROR_TITLE_STRING[] = L"Error";
}
// Gets position of given window.
std::optional<RECT> get_window_pos(HWND hwnd);
@@ -16,7 +23,7 @@ bool is_system_window(HWND hwnd, const char* class_name);
int run_message_loop(const bool until_idle = false, const std::optional<uint32_t> timeout_seconds = {});
std::optional<std::wstring> get_last_error_message(const DWORD dw);
void show_last_error_message(LPCWSTR lpszFunction, DWORD dw);
void show_last_error_message(LPCWSTR lpszFunction, DWORD dw, LPCWSTR errorTitle = localized_strings::LAST_ERROR_TITLE_STRING);
enum WindowState
{

View File

@@ -4,6 +4,7 @@
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
#include <common/debug_control.h>
#include <common/common.h>
using namespace interop;
using namespace System::Runtime::InteropServices;
@@ -46,7 +47,8 @@ void KeyboardHook::Start()
0);
if (hookHandle == nullptr)
{
throw std::exception("SetWindowsHookEx failed.");
DWORD errorCode = GetLastError();
show_last_error_message(L"SetWindowsHookEx", errorCode, L"PowerToys - Interop");
}
}
}