-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample2.c
366 lines (330 loc) · 13 KB
/
example2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* This example is licensed under a Creative Commons Zero v1.0 Universal License
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <Shlwapi.h>
#include <strsafe.h>
#include "wintoastlibc.h"
#define RETURN_GREATER_OR_EQUAL(osvi, major, minor, build) \
do { \
if((osvi).dwMajorVersion > (major)) \
return TRUE; \
if((osvi).dwMajorVersion < (major)) \
return FALSE; \
if((osvi).dwMinorVersion > (minor)) \
return TRUE; \
if((osvi).dwMinorVersion < (minor)) \
return FALSE; \
if((osvi).dwBuildNumber > (build)) \
return TRUE; \
if((osvi).dwBuildNumber < (build)) \
return FALSE; \
return TRUE; \
} while(0)
static BOOL GreaterOrEqualRTL(DWORD major, DWORD minor, DWORD build)
{
typedef LONG(WINAPI * RtlGetVersion_t)(PRTL_OSVERSIONINFOW);
HMODULE hNtdll = GetModuleHandle(TEXT("ntdll.dll"));
if(hNtdll)
{
RtlGetVersion_t RtlGetVersion_f = (RtlGetVersion_t)GetProcAddress(hNtdll, "RtlGetVersion");
if(RtlGetVersion_f)
{
RTL_OSVERSIONINFOW rosvi;
ZeroMemory(&rosvi, sizeof(rosvi));
rosvi.dwOSVersionInfoSize = sizeof(rosvi);
if(RtlGetVersion_f(&rosvi) == 0)
RETURN_GREATER_OR_EQUAL(rosvi, major, minor, build);
return FALSE;
}
return FALSE;
}
return FALSE;
}
static BOOL GreaterOrEqualK32(DWORD major, DWORD minor, DWORD build)
{
OSVERSIONINFOW osvi;
ZeroMemory(&osvi, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
if(GetVersionExW(&osvi))
RETURN_GREATER_OR_EQUAL(osvi, major, minor, build);
return FALSE;
}
static BOOL GreaterOrEqual(DWORD major, DWORD minor, DWORD build)
{
return GreaterOrEqualRTL(major, minor, build) || GreaterOrEqualK32(major, minor, build);
}
static BOOL ValidAUMIDRequired(void)
{
/* Show notifications without registered AUMID is available since Windows 10 build 1909 */
return !GreaterOrEqual(10, 0, 18363);
}
static BOOL ShortcutAUMIDRequired(void)
{
/* Show notifications without shortcut with AUMID is available since Windows 10 */
return !GreaterOrEqual(10, 0, 0);
}
static BOOL RegisterBasicAUMID(LPCWSTR aumid, LPCWSTR displayName, LPCWSTR iconUri)
{
/*
* If you need more complex example, see https://hg.mozilla.org/mozilla-central/file/d9c24252e2b25e4b3eaecfbf6110c27539e47dcd/widget/windows/ToastNotification.cpp#l245
*
* HKEY_CURRENT_USER\Software\Classes\AppID\{GUID} DllSurrogate : REG_SZ = ""
* \AppUserModelId\{MOZ_TOAST_APP_NAME}PortableToast-{install hash} CustomActivator : REG_SZ = {GUID}
* DisplayName : REG_EXPAND_SZ = {display name}
* IconUri : REG_EXPAND_SZ = {icon path}
* \CLSID\{GUID} AppID : REG_SZ = {GUID}
* \InprocServer32 (Default) : REG_SZ = {notificationserver.dll path}
*/
LONG ret;
DWORD type;
HKEY aumidRoot, aumidKey;
aumidRoot = NULL;
ret = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Classes\\AppUserModelId", 0, KEY_QUERY_VALUE, &aumidRoot);
if(ret == ERROR_FILE_NOT_FOUND)
{
aumidRoot = NULL;
ret = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Classes\\AppUserModelId", 0, NULL, 0, KEY_WRITE, NULL, &aumidRoot, NULL);
if(ret != ERROR_SUCCESS)
{
RegCloseKey(aumidRoot);
return FALSE;
}
}
else if(ret != ERROR_SUCCESS)
{
return FALSE;
}
aumidKey = NULL;
ret = RegOpenKeyExW(aumidRoot, aumid, 0, KEY_QUERY_VALUE, &aumidKey);
if(ret == ERROR_FILE_NOT_FOUND)
{
aumidKey = NULL;
ret = RegCreateKeyExW(aumidRoot, aumid, 0, NULL, 0, KEY_WRITE, NULL, &aumidKey, NULL);
if(ret != ERROR_SUCCESS)
{
RegCloseKey(aumidKey);
RegCloseKey(aumidRoot);
return FALSE;
}
}
else if(ret != ERROR_SUCCESS)
{
RegCloseKey(aumidRoot);
return FALSE;
}
if(RegQueryValueExW(aumidKey, L"DisplayName", 0, &type, NULL, NULL) != ERROR_SUCCESS || type != REG_SZ)
RegSetValueExW(aumidKey, L"DisplayName", 0, REG_SZ, (LPBYTE)displayName, (wcslen(displayName) + 1) * sizeof(wchar_t));
if(RegQueryValueExW(aumidKey, L"IconUri", 0, &type, NULL, NULL) != ERROR_SUCCESS || type != REG_SZ)
RegSetValueExW(aumidKey, L"IconUri", 0, REG_SZ, (LPBYTE)iconUri, (wcslen(iconUri) + 1) * sizeof(wchar_t));
RegCloseKey(aumidKey);
RegCloseKey(aumidRoot);
return TRUE;
}
typedef struct _ToastHandlerContext
{
HANDLE event;
WCHAR message[128];
} ToastHandlerContext;
static void WTLCAPI OnToastActivated(void * userData)
{
ToastHandlerContext * ctx = (ToastHandlerContext *)(userData);
StringCbCopyW(ctx->message, sizeof(ctx->message), L"Toast Activated");
SetEvent(ctx->event);
}
static void WTLCAPI OnToastActivatedAction(void * userData, int actionIndex)
{
ToastHandlerContext * ctx = (ToastHandlerContext *)(userData);
StringCbPrintfW(ctx->message, sizeof(ctx->message), L"Toast Activated with Action #%d", actionIndex);
SetEvent(ctx->event);
}
static void WTLCAPI OnToastDismissed(void * userData, WTLC_DismissalReason state)
{
ToastHandlerContext * ctx = (ToastHandlerContext *)(userData);
WCHAR reason[32];
switch(state)
{
case WTLC_DismissalReason_UserCanceled:
StringCbCopyW(reason, sizeof(reason), L"UserCanceled");
break;
case WTLC_DismissalReason_ApplicationHidden:
StringCbCopyW(reason, sizeof(reason), L"ApplicationHidden");
break;
case WTLC_DismissalReason_TimedOut:
StringCbCopyW(reason, sizeof(reason), L"TimedOut");
break;
default:
StringCbPrintfW(reason, sizeof(reason), L"Invalid (%d)", state);
break;
}
StringCbCopyW(ctx->message, sizeof(ctx->message), L"Toast Dismissed with DismissalReason = ");
StringCbCatW(ctx->message, sizeof(ctx->message), reason);
SetEvent(ctx->event);
}
static void WTLCAPI OnToastFailed(void * userData)
{
ToastHandlerContext * ctx = (ToastHandlerContext *)(userData);
StringCbCopyW(ctx->message, sizeof(ctx->message), L"Toast Failed");
SetEvent(ctx->event);
}
int main(int argc, char ** argv)
{
WTLC_Instance * instance = NULL;
WTLC_Template * templ = NULL;
WTLC_Error error = WTLC_Error_NoError;
LPCWSTR imagePath = L"C:\\ProgramData\\Microsoft\\User Account Pictures\\guest.png";
BOOL withImage = PathFileExistsW(imagePath);
ToastHandlerContext handlerContext;
const ULONGLONG loopTimeout = 31000; /* 31 seconds */
ULONGLONG loopStartTime;
if(!WTLC_isCompatible())
{
MessageBoxW(NULL, L"Your system is not compatible!", L"Error", MB_OK | MB_ICONERROR);
return EXIT_FAILURE;
}
if(FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
{
MessageBoxW(NULL, L"COM library initialization failed!", L"Error", MB_OK | MB_ICONERROR);
return EXIT_FAILURE;
}
instance = WTLC_Instance_Create();
if(!instance)
{
MessageBoxW(NULL, L"WinToast instance creation failed!", L"Error", MB_OK | MB_ICONERROR);
CoUninitialize();
return EXIT_FAILURE;
}
WTLC_setAppName(instance, L"Example");
if(ShortcutAUMIDRequired())
WTLC_setAppUserModelId(instance, L"Microsoft.Windows.Explorer");
else if(RegisterBasicAUMID(L"ExampleToast", L"Example", imagePath))
WTLC_setAppUserModelId(instance, L"ExampleToast");
else if(ValidAUMIDRequired())
WTLC_setAppUserModelId(instance, L"Microsoft.Windows.Explorer");
else
WTLC_setAppUserModelId(instance, L"Example");
WTLC_setShortcutPolicy(instance, WTLC_SHORTCUT_POLICY_IGNORE);
if(!WTLC_initialize(instance, &error))
{
MessageBoxW(NULL, WTLC_strerror(error), L"Error", MB_OK | MB_ICONERROR);
WTLC_Instance_Destroy(instance);
CoUninitialize();
return EXIT_FAILURE;
}
templ = WTLC_Template_Create(withImage ? WTLC_TemplateType_ImageAndText02 : WTLC_TemplateType_Text02);
WTLC_Template_setTextField(templ, L"HELLO, WORLD!", WTLC_TextField_FirstLine);
WTLC_Template_setSecondLine(templ, L"This is a test notification");
WTLC_Template_setAudioOption(templ, WTLC_AudioOption_Default);
WTLC_Template_setExpiration(templ, 30000);
if(withImage)
WTLC_Template_setImagePath(templ, imagePath);
WTLC_Template_addAction(templ, L"Action #0");
WTLC_Template_addAction(templ, L"Action #1");
ZeroMemory(&handlerContext, sizeof(handlerContext));
handlerContext.event = CreateEventW(NULL, TRUE, FALSE, NULL);
if(!handlerContext.event)
{
const DWORD lastError = GetLastError();
LPWSTR lastErrorMsgBuf = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lastErrorMsgBuf, 0, NULL);
MessageBoxW(NULL, lastErrorMsgBuf, L"Error", MB_OK | MB_ICONERROR);
LocalFree(lastErrorMsgBuf);
WTLC_Template_Destroy(templ);
WTLC_Instance_Destroy(instance);
CoUninitialize();
return EXIT_FAILURE;
}
if(WTLC_showToast(instance,
templ,
&handlerContext,
&OnToastActivated,
&OnToastActivatedAction,
&OnToastDismissed,
&OnToastFailed,
&error) < 0)
{
MessageBoxW(NULL, WTLC_strerror(error), L"Error", MB_OK | MB_ICONERROR);
CloseHandle(handlerContext.event);
WTLC_Template_Destroy(templ);
WTLC_Instance_Destroy(instance);
CoUninitialize();
return EXIT_FAILURE;
}
loopStartTime = GetTickCount64();
while(TRUE)
{
const ULONGLONG loopElapsedTime = GetTickCount64() - loopStartTime;
DWORD waitTime = 0;
DWORD waitResult = 0;
if(loopElapsedTime >= loopTimeout)
{
MessageBoxW(NULL, L"Timeout was reached without callback", L"Error", MB_OK | MB_ICONERROR);
break;
}
waitTime = (DWORD)(loopTimeout - loopElapsedTime);
/* Use MsgWaitForMultipleObjects to wait for messages or event */
waitResult = MsgWaitForMultipleObjects(1, &handlerContext.event, FALSE, waitTime, QS_ALLINPUT);
if(waitResult == WAIT_OBJECT_0)
{
/* Event object is signaled */
WCHAR message[192];
StringCbCopyW(message, sizeof(message), L"Callback was called with message:\n\n");
StringCbCatW(message, sizeof(message), handlerContext.message);
MessageBoxW(NULL, message, L"Handler", MB_OK | MB_ICONASTERISK);
break;
}
else if(waitResult == WAIT_OBJECT_0 + 1)
{
/* There are messages in queue */
BOOL loopIsDone = FALSE;
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
loopIsDone = TRUE;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(loopIsDone)
break;
}
else if(waitResult == WAIT_TIMEOUT)
{
MessageBoxW(NULL, L"Timeout was reached without callback", L"Error", MB_OK | MB_ICONERROR);
break;
}
else if(waitResult == WAIT_FAILED)
{
const DWORD lastError = GetLastError();
LPWSTR lastErrorMsgBuf = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lastErrorMsgBuf, 0, NULL);
MessageBoxW(NULL, lastErrorMsgBuf, L"Error", MB_OK | MB_ICONERROR);
LocalFree(lastErrorMsgBuf);
break;
}
else
{
WCHAR message[64];
StringCbPrintfW(message, sizeof(message), L"Unexpected waitResult = 0x%08lX", waitResult);
MessageBoxW(NULL, message, L"Error", MB_OK | MB_ICONERROR);
break;
}
}
CloseHandle(handlerContext.event);
WTLC_Template_Destroy(templ);
WTLC_Instance_Destroy(instance);
CoUninitialize();
return EXIT_SUCCESS;
}
INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, INT nCmdShow)
{
return main(0, NULL);
}