Skip to content
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

Switch from wstring errors to stacktrace exceptions #11

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions audio-router-gui/SysTray.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "SysTray.h"
#include "window.h"
#include "stacktrace\stack_exception.hpp"

using namespace std;
using namespace stacktrace;

// TODO/wolfreak99: Create a common.h for stuff such as this
#include <assert.h>
Expand Down Expand Up @@ -46,10 +50,8 @@ BOOL SysTray::SetIcon(HICON hNewIcon)

return 1;
}
catch (std::wstring err) {
catch (const exception & err) {
throw err;
assert(false);
return 0;
}
} // SetIcon

Expand Down Expand Up @@ -77,10 +79,8 @@ BOOL SysTray::SetTipText(ATL::CString newTipText)

return 1;
}
catch (std::wstring err) {
catch (const exception & err) {
throw err;
assert(false);
return 0;
}
} // SetTipText

Expand Down
12 changes: 9 additions & 3 deletions audio-router-gui/app_inject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>
#include <cassert>
#include "stacktrace\stack_exception.hpp"

using namespace std;
using namespace stacktrace;

#define MAKE_SESSION_GUID_AND_FLAG(guid, flag)\
((((DWORD)flag) << (sizeof(DWORD) * 8 - 2)) | (((DWORD)guid) & ~(3 << (sizeof(DWORD) * 8 - 2))))
Expand Down Expand Up @@ -172,14 +176,15 @@ void app_inject::inject(DWORD process_id, bool x86, size_t device_index, flush_t
try {
this->inject_dll(process_id, x86);
}
catch (std::wstring err) {
catch (const exception & err) {
throw err;
}
if (flush == SOFT) {
reset_all_devices(false);
}
else if (flush == HARD && !reset_all_devices(true)) {
throw std::wstring(L"Stream flush in target process failed.\n");
throw stack_runtime_error(
"Stream flush in target process failed.\n");
}

return;
Expand Down Expand Up @@ -272,7 +277,8 @@ void app_inject::inject_dll(DWORD pid, bool x86, DWORD tid, DWORD flags)
else {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
throw std::wstring(L"Audio Router delegate did not respond in time.\n");
throw stack_runtime_error(
"Audio Router delegate did not respond in time.\n");
}

CloseHandle(pi.hProcess);
Expand Down
6 changes: 6 additions & 0 deletions audio-router-gui/audio-router-gui.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
<ClInclude Include="app_inject.h" />
<ClInclude Include="app_list.h" />
<ClInclude Include="bootstrapper.h" />
<ClInclude Include="stacktrace\call_stack.hpp" />
<ClInclude Include="stacktrace\StackWalker\StackWalker.h" />
<ClInclude Include="stacktrace\stack_exception.hpp" />
<ClInclude Include="SysTray.h" />
<ClInclude Include="DialogMessageHook.h" />
<ClInclude Include="dialog_array.h" />
Expand All @@ -188,6 +191,9 @@
<ClCompile Include="app_inject.cpp" />
<ClCompile Include="app_list.cpp" />
<ClCompile Include="bootstrapper.cpp" />
<ClCompile Include="stacktrace\call_stack_gcc.cpp" />
<ClCompile Include="stacktrace\call_stack_msvc.cpp" />
<ClCompile Include="stacktrace\StackWalker\StackWalker.cpp" />
<ClCompile Include="SysTray.cpp" />
<ClCompile Include="DialogMessageHook.cpp" />
<ClCompile Include="dialog_array.cpp" />
Expand Down
18 changes: 18 additions & 0 deletions audio-router-gui/audio-router-gui.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
<ClInclude Include="SysTray.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stacktrace\stack_exception.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stacktrace\call_stack.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stacktrace\StackWalker\StackWalker.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="audio-router-gui.rc">
Expand Down Expand Up @@ -127,6 +136,15 @@
<ClCompile Include="SysTray.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="stacktrace\call_stack_gcc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="stacktrace\call_stack_msvc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="stacktrace\StackWalker\StackWalker.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="icon1.ico">
Expand Down
57 changes: 35 additions & 22 deletions audio-router-gui/bootstrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
#include "util.h"
#include <cassert>
#include <Psapi.h>
#include "stacktrace\call_stack.hpp"
#include "stacktrace\stack_exception.hpp"

using namespace std;
using namespace stacktrace;

#define LOCAL_PARAMS_FILE L"saved_routings.dat"
#define LOCAL_PARAMS_FILE_STR "saved_routings.dat"
#ifdef _DEBUG
# define SET_FILTERS() { filters.push_back(L"explorer.exe"); /*filters.push_back(L"steam.exe");*/ }
#else
Expand Down Expand Up @@ -55,6 +61,7 @@ void try_loading(HANDLE hfile, global_routing_params *& params, LARGE_INTEGER si

unsigned char *view_of_file = (unsigned char *)MapViewOfFile(hfile, FILE_MAP_READ, 0, 0, 0);
unsigned char *buffer = NULL;

__try
{
buffer = new unsigned char[size.QuadPart];
Expand All @@ -79,8 +86,9 @@ void try_loading(HANDLE hfile, global_routing_params *& params, LARGE_INTEGER si
}

params = NULL;
throw std::wstring(L"The " LOCAL_PARAMS_FILE L" file is corrupted. Please delete the "\
L"file and open Audio Router again.\n");

throw wstring(L"The " LOCAL_PARAMS_FILE L" file is corrupted. Please delete the "\
L"file and open Audio Router again.\n");
}
} // try_loading

Expand All @@ -90,12 +98,10 @@ void bootstrapper::load_local_implicit_params(bool create_default_if_empty)
FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));

if (this->local_file == INVALID_HANDLE_VALUE) {
throw std::wstring(L"Could not open or create a new file in the working directory.\n");
throw stack_runtime_error("Could not open or create a new file in the working directory.\n");
}

LARGE_INTEGER size = {
0
};
LARGE_INTEGER size = {0};
size.QuadPart = 1;
GetFileSizeEx(this->local_file, &size);

Expand All @@ -109,11 +115,17 @@ void bootstrapper::load_local_implicit_params(bool create_default_if_empty)
this->load_local_implicit_params(false);
}
else if (this->implicit_params == NULL) {
throw std::wstring(L"The " LOCAL_PARAMS_FILE L" file is empty.\n");
throw stack_runtime_error("The " LOCAL_PARAMS_FILE_STR " file is empty.\n");
}
else {
// rebase and validate the view
try_loading(this->implicit_params, this->routing_params, size);
try {
// rebase and validate the view
try_loading(this->implicit_params, this->routing_params, size);
}
catch (std::wstring errmsg_wstr) {
std::string errmsg_str = wstring_to_string(errmsg_wstr);
throw stack_runtime_error(errmsg_str);
}
}
} // load_local_implicit_params

Expand Down Expand Up @@ -178,7 +190,7 @@ bool bootstrapper::is_managed_app(DWORD pid) const
void bootstrapper::save_routing(DWORD pid, IMMDevice *dev)
{
if (!dev) {
throw std::wstring(L"Saving routing to Default Device is not supported.");
throw stack_runtime_error("Saving routing to Default Device is not supported.");
}

if (!this->routing_params) {
Expand Down Expand Up @@ -225,7 +237,7 @@ void bootstrapper::save_routing(DWORD pid, IMMDevice *dev)
try {
this->update_save();
}
catch (std::wstring err) {
catch (const exception & err) {
*params = NULL;
CoTaskMemFree(id);
throw err;
Expand All @@ -252,7 +264,7 @@ void bootstrapper::update_save()
assert(routing_params_size > 0);

if (routing_params_size == 0) {
throw std::wstring(L"The internal state of saved routings is corrupted.\n");
throw stack_runtime_error("The internal state of saved routings is corrupted.\n");
}

// update the file and create a view
Expand All @@ -267,18 +279,19 @@ void bootstrapper::update_save()
} while (this->local_file == INVALID_HANDLE_VALUE && GetLastError() == ERROR_USER_MAPPED_FILE);

if (this->local_file == INVALID_HANDLE_VALUE) {
throw std::wstring(
L"Could not open file " LOCAL_PARAMS_FILE L" while saving routing. "\
L"Audio Router is left in an invalid state and must be restarted.\n");
throw stack_runtime_error(
"Could not open file " LOCAL_PARAMS_FILE_STR " while saving routing. "\
"Audio Router is left in an invalid state and must be restarted.\n");
}

this->implicit_params.Attach(CreateFileMapping(this->local_file, NULL,
PAGE_READWRITE, 0, (DWORD)routing_params_size, NULL));

if (!this->implicit_params) {
this->local_file.Close();
throw std::wstring(L"Unexpected error occured. Old routed savings data is lost. "\
L"Audio Router is left in an invalid state and must be restarted.\n");
throw stack_runtime_error(
"Unexpected error occured. Old routed savings data is lost. "\
"Audio Router is left in an invalid state and must be restarted.\n");
}

unsigned char *view_of_file = (unsigned char *)MapViewOfFile(this->implicit_params,
Expand All @@ -287,9 +300,9 @@ void bootstrapper::update_save()
if (!view_of_file) {
this->implicit_params.Close();
this->local_file.Close();
throw std::wstring(
L"Unexpected error occured while mapping. Old routed savings data "\
L"is lost. Audio Router is left in an invalid state and must be restarted.\n");
throw stack_runtime_error(
"Unexpected error occured while mapping. Old routed savings data "\
"is lost. Audio Router is left in an invalid state and must be restarted.\n");
}

serialize(routing_params, view_of_file);
Expand Down Expand Up @@ -365,7 +378,7 @@ bootstrapper::bootstrapper(HWND hwnd) : available(false), hwnd(hwnd), routing_pa
app_inject::inject_dll(it->id, it->x86, 0, 3);
this->available = true;
}
catch (std::wstring err) {}
catch (const exception & err) { }
}

if (!this->available) {
Expand Down Expand Up @@ -393,7 +406,7 @@ bootstrapper::~bootstrapper()
try {
app_inject::inject_dll(it->id, it->x86, 0, 3);
}
catch (std::wstring err) {
catch (const exception & err) {
#ifdef _DEBUG
MessageBox(NULL, L"bootstrapper couldn't be notified", NULL, MB_ICONERROR);
#endif
Expand Down
11 changes: 8 additions & 3 deletions audio-router-gui/delegation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "app_list.h"
#include "app_inject.h"
#include <cassert>
#include "stacktrace\stack_exception.hpp"

using namespace std;
using namespace stacktrace;

delegation::delegation()
{
Expand Down Expand Up @@ -57,15 +61,16 @@ DWORD delegation::pipe_listener_proc(LPVOID arg)
assert(b);
assert(written == OUTPUT_SIZE);
}
catch (std::wstring err) {
catch (const exception & err) {
BOOL b = WriteFile(hpipe, &exitcode, OUTPUT_SIZE, &written, NULL);
FlushFileBuffers(hpipe);
assert(b);
assert(written == OUTPUT_SIZE);

// TODO/audiorouterdev: limit messagebox to 1 instance
err += L"Routing functionality couldn't be initialized in the starting process.";
MessageBox(NULL, err.c_str(), NULL, MB_ICONERROR);
wstring errmsg = string_to_wstring(err.what());
errmsg += L"Routing functionality couldn't be initialized in the starting process.";
MessageBox(NULL, errmsg.c_str(), NULL, MB_ICONERROR);
}
}

Expand Down
22 changes: 15 additions & 7 deletions audio-router-gui/dialog_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <Audioclient.h>
#include <Psapi.h>
#include <cassert>
#include "stacktrace\stack_exception.hpp"
#include "util.h"

using namespace std;
using namespace stacktrace;

#define IDD_TIMER_CONTROL_DLG 2
#define TIMER_INTERVAL_CONTROL_DLG 10
Expand Down Expand Up @@ -621,9 +626,10 @@ void dialog_control::do_route(bool duplication)
injector.populate_devicelist();
injector.inject(this->pid, this->x86, sel_index, flush, duplication);
}
catch (std::wstring err) {
err += L"Router functionality not available.";
this->MessageBoxW(err.c_str(), NULL, MB_ICONERROR);
catch (const exception & err) {
wstring errmsg = string_to_wstring(err.what());
errmsg += L"\nRouter functionality not available.";
this->MessageBoxW(errmsg.c_str(), NULL, MB_ICONERROR);
return;
}
this->routing_state = (sel_index != 0 ? ROUTING : NO_STATE);
Expand Down Expand Up @@ -885,9 +891,10 @@ LRESULT dialog_control::OnPopUpSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*h
try {
bootstrap.save_routing(this->pid, devices[sel_index - 1]);
}
catch (std::wstring err) {
catch (const exception & err) {
app_inject::clear_devices(devices);
this->MessageBoxW(err.c_str(), NULL, MB_ICONERROR);
wstring errmsg = string_to_wstring(err.what());
this->MessageBoxW(errmsg.c_str(), NULL, MB_ICONERROR);
return 0;
}
app_inject::clear_devices(devices);
Expand Down Expand Up @@ -938,8 +945,9 @@ LRESULT dialog_control::OnPopUpDelete(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /
return 0;
}
}
catch (std::wstring err) {
this->MessageBoxW(err.c_str(), NULL, MB_ICONERROR);
catch (const exception & err) {
wstring errmsg = string_to_wstring(err.what());
this->MessageBoxW(errmsg.c_str(), NULL, MB_ICONERROR);
return 0;
}
dialog_main& main = this->parent.parent;
Expand Down
12 changes: 9 additions & 3 deletions audio-router-gui/formview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include "policy_config.h"
#include <cstring>
#include <cassert>
#include "stacktrace\stack_exception.hpp"
#include "util.h"

using namespace std;
using namespace stacktrace;

#ifndef DISABLE_TELEMETRY
extern telemetry *telemetry_m;
Expand Down Expand Up @@ -209,9 +214,10 @@ void formview::open_dialog()
injector.populate_devicelist();
injector.inject(app.id, app.x86, sel_index, flush);
}
catch (std::wstring err) {
err += L"Router functionality not available.";
this->MessageBoxW(err.c_str(), NULL, MB_ICONERROR);
catch (const exception & err) {
wstring errmsg = string_to_wstring(err.what());
errmsg += L"\nRouter functionality not available.";
this->MessageBoxW(errmsg.c_str(), NULL, MB_ICONERROR);
return;
}
// set names
Expand Down
Loading