10
10
#else
11
11
#define DEBUG_MSG (str ) do { std::wcout << str << std::endl; } while ( false )
12
12
#endif
13
-
14
13
using namespace WinToastLib ;
15
14
namespace DllImporter {
16
15
@@ -64,6 +63,30 @@ namespace DllImporter {
64
63
}
65
64
}
66
65
66
+ class WinToastStringWrapper {
67
+ public:
68
+ WinToastStringWrapper (_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw () {
69
+ HRESULT hr = DllImporter::WindowsCreateStringReference (stringRef, length, &_header, &_hstring);
70
+ if (!SUCCEEDED (hr)) {
71
+ RaiseException (static_cast <DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0 , nullptr );
72
+ }
73
+ }
74
+ WinToastStringWrapper (_In_ const std::wstring &stringRef) throw () {
75
+ HRESULT hr = DllImporter::WindowsCreateStringReference (stringRef.c_str (), static_cast <UINT32>(stringRef.length ()), &_header, &_hstring);
76
+ if (FAILED (hr)) {
77
+ RaiseException (static_cast <DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0 , nullptr );
78
+ }
79
+ }
80
+ ~WinToastStringWrapper () {
81
+ DllImporter::WindowsDeleteString (_hstring);
82
+ }
83
+ inline HSTRING Get () const throw() { return _hstring; }
84
+ private:
85
+ HSTRING _hstring;
86
+ HSTRING_HEADER _header;
87
+
88
+ };
89
+
67
90
namespace Util {
68
91
inline HRESULT defaultExecutablePath (_In_ WCHAR* path, _In_ DWORD nSize = MAX_PATH) {
69
92
DWORD written = GetModuleFileNameEx (GetCurrentProcess (), nullptr , path, nSize);
@@ -109,7 +132,7 @@ namespace Util {
109
132
return hr;
110
133
}
111
134
112
- inline HRESULT setEventHandlers (_In_ IToastNotification* notification, _In_ std::shared_ptr<WinToastHandler > eventHandler) {
135
+ inline HRESULT setEventHandlers (_In_ IToastNotification* notification, _In_ std::shared_ptr<IWinToastHandler > eventHandler) {
113
136
EventRegistrationToken activatedToken, dismissedToken, failedToken;
114
137
HRESULT hr = notification->add_Activated (
115
138
Callback < Implements < RuntimeClassFlags<ClassicCom>,
@@ -128,7 +151,7 @@ namespace Util {
128
151
ToastDismissalReason reason;
129
152
if (SUCCEEDED (e->get_Reason (&reason)))
130
153
{
131
- eventHandler->toastDismissed (static_cast <WinToastHandler ::WinToastDismissalReason>(reason));
154
+ eventHandler->toastDismissed (static_cast <IWinToastHandler ::WinToastDismissalReason>(reason));
132
155
}
133
156
return S_OK;
134
157
}).Get (), &dismissedToken);
@@ -146,6 +169,8 @@ namespace Util {
146
169
}
147
170
}
148
171
172
+
173
+
149
174
WinToast* WinToast::_instance = nullptr ;
150
175
WinToast* WinToast::instance () {
151
176
if (_instance == nullptr ) {
@@ -159,6 +184,9 @@ WinToast::WinToast() : _isInitialized(false)
159
184
DllImporter::initialize ();
160
185
}
161
186
187
+ WinToast::~WinToast () {
188
+ }
189
+
162
190
void WinToast::setAppName (_In_ const std::wstring& appName) {
163
191
_appName = appName;
164
192
}
@@ -338,7 +366,7 @@ HRESULT WinToast::createShellLink() {
338
366
339
367
340
368
341
- bool WinToast::showToast (_In_ const WinToastTemplate& toast, _In_ WinToastHandler * handler) {
369
+ bool WinToast::showToast (_In_ const WinToastTemplate& toast, _In_ IWinToastHandler * handler) {
342
370
if (!isInitialized ()) {
343
371
DEBUG_MSG (" Error when launching the toast. WinToast is not initialized =(" );
344
372
return _isInitialized;
@@ -355,7 +383,7 @@ bool WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ WinToastHandl
355
383
if (SUCCEEDED (hr)) {
356
384
hr = _notificationFactory->CreateToastNotification (xmlDocument (), &_notification);
357
385
if (SUCCEEDED (hr)) {
358
- hr = Util::setEventHandlers (notification (), std::shared_ptr<WinToastHandler >(handler));
386
+ hr = Util::setEventHandlers (notification (), std::shared_ptr<IWinToastHandler >(handler));
359
387
if (SUCCEEDED (hr)) {
360
388
hr = _notifier->Show (notification ());
361
389
}
@@ -413,7 +441,6 @@ WinToastTemplate::WinToastTemplate(const WinToastTemplateType& type) :
413
441
initComponentsFromType ();
414
442
}
415
443
416
-
417
444
WinToastTemplate::~WinToastTemplate ()
418
445
{
419
446
_textFields.clear ();
@@ -433,55 +460,3 @@ void WinToastTemplate::initComponentsFromType() {
433
460
_hasImage = _type < Text01;
434
461
_textFields = std::vector<std::wstring>(TextFieldsCount[_type], L" " );
435
462
}
436
-
437
-
438
- void WinToastHandler::toastActivated () const {
439
- std::wcout << L" The user clicked in this toast" << std::endl;
440
-
441
- }
442
-
443
- void WinToastHandler::toastFailed () const {
444
- std::wcout << L" Error showing current toast" << std::endl;
445
- }
446
-
447
- void WinToastHandler::toastDismissed (WinToastHandler::WinToastDismissalReason state) const {
448
- switch (state) {
449
- case UserCanceled:
450
- std::wcout << L" The user dismissed this toast" << std::endl;
451
- break ;
452
- case ApplicationHidden:
453
- std::wcout << L" The application hid the toast using ToastNotifier.hide()" << std::endl;
454
- break ;
455
- case TimedOut:
456
- std::wcout << L" The toast has timed out" << std::endl;
457
- break ;
458
- default :
459
- std::wcout << L" Toast not activated" << std::endl;
460
- break ;
461
- }
462
- }
463
-
464
-
465
- WinToastStringWrapper::WinToastStringWrapper (_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw() {
466
- HRESULT hr = DllImporter::WindowsCreateStringReference (stringRef, length, &_header, &_hstring);
467
- if (!SUCCEEDED (hr)) {
468
- RaiseException (static_cast <DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0 , nullptr );
469
- }
470
- }
471
-
472
-
473
- WinToastStringWrapper::WinToastStringWrapper (const std::wstring &stringRef)
474
- {
475
- HRESULT hr = DllImporter::WindowsCreateStringReference (stringRef.c_str (), static_cast <UINT32>(stringRef.length ()), &_header, &_hstring);
476
- if (FAILED (hr)) {
477
- RaiseException (static_cast <DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0 , nullptr );
478
- }
479
- }
480
-
481
- WinToastStringWrapper::~WinToastStringWrapper () {
482
- DllImporter::WindowsDeleteString (_hstring);
483
- }
484
-
485
- HSTRING WinToastStringWrapper::Get () const throw() {
486
- return _hstring;
487
- }
0 commit comments