source: trunk/Utilities/ShowHresult/NotifyIconWindow.h @ 937

Last change on this file since 937 was 313, checked in by roman, 10 years ago
  • Property svn:keywords set to Id
File size: 27.1 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: NotifyIconWindow.h 313 2014-08-31 08:21:21Z roman $
6
7#pragma once
8
9#include <vfwmsgs.h>
10#include <vfw.h> // FACILITY_ITF, AVIERR_
11#include <asferr.h> // FACILITY_NS
12#include <nserror.h> // FACILITY_NS
13#include <mferror.h> // FACILITY_MF
14#include <wmcodecdsp.h> // FACILITY_WMAAECMA
15#include <ddraw.h> // _FACD3D
16//#include <d3d9helper.h> // _FACD3D
17#include <d3d9.h> // _FACD3D
18#include <d2derr.h> // FACILITY_D2D
19#include <wincodec.h> // FACILITY_WINCODEC_ERR
20#include <wia_lh.h> // FACILITY_WIA
21#include <corerror.h> // FACILITY_URT
22#include <audioclient.h> // FACILITY_AUDCLNT
23#include <p2p.h> // FACILITY_P2P
24#include <slerror.h> // FACILITY_SL_ITF
25
26#include <adserr.h>
27#include <azroles.h>
28#include <callobj.h>
29#include <cryptxml.h>
30#include <functiondiscoveryerror.h>
31#include <infotech.h>
32//#include <naperror.h> - merged with winerror.h in SDK 8.0
33//#include <pstore.h> - missing in SDK 8.0
34#include <sherrors.h>
35#include <ShObjIdl.h>
36#include <SubsMgr.h>
37#include <UrlMon.h>
38#include <wcmerrors.h>
39
40#include "rowinhttp.h"
41#include "AboutDialog.h"
42
43////////////////////////////////////////////////////////////
44// CNotifyIconWindow
45
46class CNotifyIconWindow : 
47        public CMessageOnlyWindowImpl<CNotifyIconWindow>
48{
49public:
50
51DECLARE_WND_CLASS(_T("CNotifyIconWindow"))
52
53BEGIN_MSG_MAP_EX(CNotifyIconWindow)
54        _Z4(atlTraceUI, 4, _T("uMsg 0x%04X, wParam 0x%08X, lParam 0x%08X\n"), uMsg, wParam, lParam);
55        //CHAIN_MSG_MAP(CMessageOnlyWindowImpl<CNotifyIconWindow>)
56        MSG_WM_CREATE(OnCreate)
57        MSG_WM_DESTROY(OnDestroy)
58        MSG_WM_CHANGECBCHAIN(OnChangeCbChain)
59        MSG_WM_DRAWCLIPBOARD(OnDrawClipboard)
60        MSG_WM_MOUSEMOVE(OnMouseMove)
61        MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
62        MSG_WM_RBUTTONUP(OnRButtonUp)
63        MESSAGE_HANDLER_EX(NIN_BALLOONUSERCLICK, OnNotifyIconBalloonUserClick)
64        MESSAGE_HANDLER_EX(WM_NOTIFYICON, OnNotifyIcon)
65        COMMAND_ID_HANDLER_EX(ID_APP_ABOUT, OnApplicationAbout)
66        COMMAND_ID_HANDLER_EX(ID_APP_EXIT, OnApplicationExit)
67        REFLECT_NOTIFICATIONS()
68END_MSG_MAP()
69
70public:
71
72        //////////////////////////////////////////////////////////
73        // Window Message Identifiers
74
75        enum
76        {
77                WM_FIRST = WM_APP,
78                WM_NOTIFYICON,
79        };
80
81private:
82        ULONGLONG m_nCommonControlsVersion;
83        ULONGLONG m_nShellVersion;
84        NOTIFYICONDATA m_NotifyIconData;
85        CWindow m_NextClipboardViewerWindow;
86        CString m_sQuery;
87
88        static BOOL IsQuartzResult(HRESULT nResult, CString* psMessage = NULL)
89        {
90                if(HRESULT_FACILITY(nResult) != FACILITY_ITF)
91                        return FALSE;
92                const SCODE nCode = HRESULT_CODE(nResult);
93                if(nCode < 0x0200)// && nCode <= 0xFFFF)
94                        return FALSE;
95                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("quartz.dll")), nResult);
96                if(sMessage.IsEmpty())
97                        return FALSE;
98                if(psMessage)
99                        *psMessage = sMessage;
100                return TRUE;
101        }
102        static BOOL LookupQuartzIdentifier(HRESULT nValue, CString& sIdentifier)
103        {
104                _A(sIdentifier.IsEmpty());
105                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
106                {
107                        #define A(x) { x, #x },
108                        #include "DsIdentifier.inc"
109                        #undef A
110                };
111                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
112                        if(g_pMap[nIndex].nValue == nValue)
113                        {
114                                sIdentifier = CString(g_pMap[nIndex].pszName);
115                                return TRUE;
116                        }
117                return FALSE;
118        }
119        static BOOL IsWmResult(HRESULT nResult, CString* psMessage = NULL)
120        {
121                if(HRESULT_FACILITY(nResult) != FACILITY_NS)
122                        return FALSE;
123                // SUGG: Cache loaded libraries
124                HMODULE hModule = NULL;
125                const SCODE nCode = HRESULT_CODE(nResult);
126                CString sMessage;
127                if(nCode >= 2000 && nCode < 3000) // 2000 -  2999 = ASF (defined in ASFERR.MC)
128                        sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("asferror.dll")), nResult);
129                else
130                        sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("wmerror.dll")), nResult);
131                if(sMessage.IsEmpty())
132                        return FALSE;
133                if(psMessage)
134                        *psMessage = sMessage;
135                return TRUE;
136        }
137        static BOOL LookupWmIdentifier(HRESULT nValue, CString& sIdentifier)
138        {
139                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
140                {
141                        #define A(x) { x, #x },
142                        #include "WmIdentifier.inc"
143                        #undef A
144                };
145                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
146                        if(g_pMap[nIndex].nValue == nValue)
147                        {
148                                sIdentifier = CString(g_pMap[nIndex].pszName);
149                                return TRUE;
150                        }
151                return FALSE;
152        }
153        static BOOL IsMfResult(HRESULT nResult, CString* psMessage = NULL)
154        {
155                if(HRESULT_FACILITY(nResult) != FACILITY_MF)
156                        return FALSE;
157                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("mferror.dll")), nResult);
158                if(sMessage.IsEmpty())
159                        return FALSE;
160                if(psMessage)
161                        *psMessage = sMessage;
162                return TRUE;
163        }
164        static BOOL LookupMfIdentifier(HRESULT nValue, CString& sIdentifier)
165        {
166                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
167                {
168                        #define A(x) { x, #x },
169                        #include "MfIdentifier.inc"
170                        #undef A
171                };
172                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
173                        if(g_pMap[nIndex].nValue == nValue)
174                        {
175                                sIdentifier = CString(g_pMap[nIndex].pszName);
176                                return TRUE;
177                        }
178                return FALSE;
179        }
180        static BOOL IsAudioClientResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
181        {
182                if(HRESULT_FACILITY(nResult) != FACILITY_AUDCLNT)
183                        return FALSE;
184                psMessage;
185                CString sIdentifier;
186                if(!LookupAudioClientIdentifier(nResult, sIdentifier))
187                        return FALSE;
188                if(psIdentifier)
189                        *psIdentifier = sIdentifier;
190                return TRUE;
191        }
192        static BOOL LookupAudioClientIdentifier(HRESULT nValue, CString& sIdentifier)
193        {
194                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
195                {
196                        #define A(x) { x, #x },
197                        #include "AudioClientIdentifier.inc"
198                        #undef A
199                };
200                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
201                        if(g_pMap[nIndex].nValue == nValue)
202                        {
203                                sIdentifier = CString(g_pMap[nIndex].pszName);
204                                return TRUE;
205                        }
206                return FALSE;
207        }
208        static BOOL IsWs2Result(HRESULT nResult, CString* psMessage = NULL)
209        {
210                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
211                        return FALSE;
212                const SCODE nCode = HRESULT_CODE(nResult);
213                if(nCode < 10000 || nCode >= 11150) // WSABASEERR, ...
214                        return FALSE;
215                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("ws2_32.dll")), nResult);
216                if(sMessage.IsEmpty())
217                        return FALSE;
218                if(psMessage)
219                        *psMessage = sMessage;
220                return TRUE;
221        }
222        static BOOL IsWinHttpResult(HRESULT nResult, CString* psMessage = NULL)
223        {
224                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
225                        return FALSE;
226                const LONG nCode = HRESULT_CODE(nResult);
227                if(nCode < 12000 || nCode >= 12200) // WINHTTP_ERROR_BASE, WINHTTP_ERROR_LAST
228                        return FALSE;
229                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("winhttp.dll")), nResult);
230                if(sMessage.IsEmpty())
231                        return FALSE;
232                if(psMessage)
233                        *psMessage = sMessage;
234                return TRUE;
235        }
236        static BOOL IsWinInetResult(HRESULT nResult, CString* psMessage = NULL)
237        {
238                if(HRESULT_FACILITY(nResult) != FACILITY_WIN32)
239                        return FALSE;
240                const LONG nCode = HRESULT_CODE(nResult);
241                if(nCode < 12000 || nCode >= 12200) // INTERNET_ERROR_BASE, INTERNET_ERROR_LAST
242                        return FALSE;
243                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("wininet.dll")), nResult);
244                if(sMessage.IsEmpty())
245                        return FALSE;
246                if(psMessage)
247                        *psMessage = sMessage;
248                return TRUE;
249        }
250        static BOOL LookupSystemIdentifier(HRESULT nValue, CString& sIdentifier)
251        {
252                if(HRESULT_SEVERITY(nValue) == SEVERITY_ERROR && HRESULT_FACILITY(nValue) == FACILITY_WIN32)
253                        nValue = HRESULT_CODE(nValue);
254                if(nValue < 0 || nValue >= 16384)
255                        return FALSE;
256                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
257                {
258                        #define A(x) { x, #x },
259                        #include "SystemIdentifier.inc"
260                        #undef A
261                };
262                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
263                        if(g_pMap[nIndex].nValue == nValue)
264                        {
265                                sIdentifier = CString(g_pMap[nIndex].pszName);
266                                return TRUE;
267                        }
268                return FALSE;
269        }
270        static BOOL LookupHresultSystemIdentifier(HRESULT nValue, CString& sIdentifier)
271        {
272                //if(HRESULT_SEVERITY(nValue) != SEVERITY_ERROR)
273                //      return _T("");
274                if((UINT) HRESULT_FACILITY(nValue) >= 0x40)
275                        return FALSE;
276                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
277                {
278                        #define A(x) { x, #x },
279                        #include "SystemHresultIdentifier.inc"
280                        #undef A
281                };
282                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
283                        if(g_pMap[nIndex].nValue == nValue)
284                        {
285                                sIdentifier = CString(g_pMap[nIndex].pszName);
286                                return TRUE;
287                        }
288                return FALSE;
289        }
290        static BOOL IsD3dResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
291        {
292                if(HRESULT_FACILITY(nResult) != _FACD3D)
293                        return FALSE;
294                psMessage;
295                CString sIdentifier;
296                if(!LookupD3dIdentifier(nResult, sIdentifier))
297                        return FALSE;
298                if(psIdentifier)
299                        *psIdentifier = sIdentifier;
300                return TRUE;
301        }
302        static BOOL LookupD3dIdentifier(HRESULT nValue, CString& sIdentifier)
303        {
304                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
305                {
306                        #define A(x) { x, #x },
307                        #include "D3dIdentifier.inc"
308                        #undef A
309                };
310                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
311                        if(g_pMap[nIndex].nValue == nValue)
312                        {
313                                sIdentifier = CString(g_pMap[nIndex].pszName);
314                                return TRUE;
315                        }
316                return FALSE;
317        }
318        static BOOL IsD2dResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
319        {
320                if(HRESULT_FACILITY(nResult) != FACILITY_D2D)
321                        return FALSE;
322                psMessage;
323                CString sIdentifier;
324                if(!LookupD2dIdentifier(nResult, sIdentifier))
325                        return FALSE;
326                if(psIdentifier)
327                        *psIdentifier = sIdentifier;
328                return TRUE;
329        }
330        static BOOL LookupD2dIdentifier(HRESULT nValue, CString& sIdentifier)
331        {
332                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
333                {
334                        #define A(x) { x, #x },
335                        #include "D2dIdentifier.inc"
336                        #undef A
337                };
338                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
339                        if(g_pMap[nIndex].nValue == nValue)
340                        {
341                                sIdentifier = CString(g_pMap[nIndex].pszName);
342                                return TRUE;
343                        }
344                return FALSE;
345        }
346        static BOOL IsWicResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
347        {
348                if(HRESULT_FACILITY(nResult) != FACILITY_WINCODEC_ERR)
349                        return FALSE;
350                psMessage;
351                CString sIdentifier;
352                if(!LookupWicIdentifier(nResult, sIdentifier))
353                        return FALSE;
354                if(psIdentifier)
355                        *psIdentifier = sIdentifier;
356                return TRUE;
357        }
358        static BOOL LookupWicIdentifier(HRESULT nValue, CString& sIdentifier)
359        {
360                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
361                {
362                        #define A(x) { x, #x },
363                        #include "WicIdentifier.inc"
364                        #undef A
365                };
366                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
367                        if(g_pMap[nIndex].nValue == nValue)
368                        {
369                                sIdentifier = CString(g_pMap[nIndex].pszName);
370                                return TRUE;
371                        }
372                return FALSE;
373        }
374        static BOOL IsWiaResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
375        {
376                if(HRESULT_FACILITY(nResult) != FACILITY_WIA)
377                        return FALSE;
378                psMessage;
379                CString sIdentifier;
380                if(!LookupWiaIdentifier(nResult, sIdentifier))
381                        return FALSE;
382                if(psIdentifier)
383                        *psIdentifier = sIdentifier;
384                return TRUE;
385        }
386        static BOOL LookupWiaIdentifier(HRESULT nValue, CString& sIdentifier)
387        {
388                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
389                {
390                        #define A(x) { x, #x },
391                        #include "WiaIdentifier.inc"
392                        #undef A
393                };
394                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
395                        if(g_pMap[nIndex].nValue == nValue)
396                        {
397                                sIdentifier = CString(g_pMap[nIndex].pszName);
398                                return TRUE;
399                        }
400                return FALSE;
401        }
402        static BOOL IsUrtResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
403        {
404                if(HRESULT_FACILITY(nResult) != FACILITY_URT)
405                        return FALSE;
406                psMessage;
407                CString sIdentifier;
408                if(!LookupUrtIdentifier(nResult, sIdentifier))
409                        return FALSE;
410                if(psIdentifier)
411                        *psIdentifier = sIdentifier;
412                return TRUE;
413        }
414        static BOOL LookupUrtIdentifier(HRESULT nValue, CString& sIdentifier)
415        {
416                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
417                {
418                        #define A(x) { x, #x },
419                        #include "UrtIdentifier.inc"
420                        #undef A
421                };
422                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
423                        if(g_pMap[nIndex].nValue == nValue)
424                        {
425                                sIdentifier = CString(g_pMap[nIndex].pszName);
426                                return TRUE;
427                        }
428                return FALSE;
429        }
430        static BOOL IsP2pResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
431        {
432                if(HRESULT_FACILITY(nResult) != FACILITY_P2P)
433                        return FALSE;
434                psMessage;
435                CString sIdentifier;
436                if(!LookupP2pIdentifier(nResult, sIdentifier))
437                        return FALSE;
438                if(psIdentifier)
439                        *psIdentifier = sIdentifier;
440                return TRUE;
441        }
442        static BOOL LookupP2pIdentifier(HRESULT nValue, CString& sIdentifier)
443        {
444                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
445                {
446                        #define A(x) { x, #x },
447                        #include "P2pIdentifier.inc"
448                        #undef A
449                };
450                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
451                        if(g_pMap[nIndex].nValue == nValue)
452                        {
453                                sIdentifier = CString(g_pMap[nIndex].pszName);
454                                return TRUE;
455                        }
456                return FALSE;
457        }
458        static BOOL IsSlResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
459        {
460                if(HRESULT_FACILITY(nResult) != FACILITY_SL_ITF)
461                        return FALSE;
462                psMessage;
463                const CString sMessage = AtlFormatSystemMessage(CDataLibraryMap::LoadLibrary(_T("slc.dll")), nResult);
464                if(psMessage)
465                        *psMessage = sMessage;
466                CString sIdentifier;
467                if(!LookupSlIdentifier(nResult, sIdentifier))
468                        if(sMessage.IsEmpty())
469                                return FALSE;
470                if(psIdentifier)
471                        *psIdentifier = sIdentifier;
472                return TRUE;
473        }
474        static BOOL LookupSlIdentifier(HRESULT nValue, CString& sIdentifier)
475        {
476                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
477                {
478                        #define A(x) { x, #x },
479                        #include "SlIdentifier.inc"
480                        #undef A
481                };
482                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
483                        if(g_pMap[nIndex].nValue == nValue)
484                        {
485                                sIdentifier = CString(g_pMap[nIndex].pszName);
486                                return TRUE;
487                        }
488                return FALSE;
489        }
490        static BOOL IsAviResult(HRESULT nResult, CString* psMessage = NULL, CString* psIdentifier = NULL)
491        {
492                if(HRESULT_FACILITY(nResult) != FACILITY_ITF && HRESULT_CODE(nResult) - (0x4000 + 100) < 100)
493                        return FALSE;
494                psMessage;
495                CString sIdentifier;
496                if(!LookupAviIdentifier(nResult, sIdentifier))
497                        return FALSE;
498                if(psIdentifier)
499                        *psIdentifier = sIdentifier;
500                return TRUE;
501        }
502        static BOOL LookupAviIdentifier(HRESULT nValue, CString& sIdentifier)
503        {
504                static const struct { HRESULT nValue; LPCSTR pszName; } g_pMap[] = 
505                {
506                        #define A(x) { x, #x },
507                        #include "AviIdentifier.inc"
508                        #undef A
509                };
510                for(SIZE_T nIndex = 0; nIndex < DIM(g_pMap); nIndex++)
511                        if(g_pMap[nIndex].nValue == nValue)
512                        {
513                                sIdentifier = CString(g_pMap[nIndex].pszName);
514                                return TRUE;
515                        }
516                return FALSE;
517        }
518
519public:
520// CNotifyIconWindow
521        static ULONGLONG GetCommonControlsVersion()
522        {
523                DWORD nMajorVersion, nMinorVersion;
524                _W(SUCCEEDED(AtlGetCommCtrlVersion(&nMajorVersion, &nMinorVersion)));
525                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
526        }
527        static ULONGLONG GetShellVersion()
528        {
529                DWORD nMajorVersion, nMinorVersion;
530                _W(SUCCEEDED(AtlGetShellVersion(&nMajorVersion, &nMinorVersion)));
531                return (ULONGLONG) ((nMajorVersion << 16) + nMinorVersion) << 32;
532        }
533        CNotifyIconWindow()
534        {
535        }
536        HWND Create()
537        {
538                return __super::Create(NULL, rcDefault, _T("AlaxInfo.ShowHresult.NotifyIconWindow"));
539        }
540        static CString GetDefaultInfoTitle()
541        {
542                return AtlLoadString(IDS_NOTIFYICON_DEFAULTTIPTITLE);
543        }
544        static DWORD GetDefaultInfoFlags()
545        {
546                return NIIF_NONE;
547        }
548        VOID SetBalloonToolTip(DWORD nFlags, LPCTSTR pszTitle, LPCTSTR pszText, UINT nTimeout = 30 * 1000)
549        {
550                if(IsWindow() && m_NotifyIconData.uFlags & NIF_INFO)
551                {
552                        m_NotifyIconData.uTimeout = nTimeout;
553                        m_NotifyIconData.dwInfoFlags = nFlags;
554                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, pszTitle, _TRUNCATE);
555                        _tcsncpy_s(m_NotifyIconData.szInfo, pszText, _TRUNCATE);
556                        _W(Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData));
557                }
558        }
559        static BOOL IsDecimal(TCHAR nCharacter)
560        {
561                return _tcschr(_T("0123456789"), nCharacter) != NULL;
562        }
563        static BOOL IsDecimal(const TCHAR* pszCharacters, SIZE_T nCount)
564        {
565                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
566                        if(!IsDecimal(pszCharacters[nIndex]))
567                                return FALSE;
568                return TRUE;
569        }
570        static BOOL IsDecimal(const TCHAR* pszCharacters)
571        {
572                return IsDecimal(pszCharacters, _tcslen(pszCharacters));
573        }
574        static BOOL IsHexadecimal(TCHAR nCharacter)
575        {
576                return _tcschr(_T("0123456789ABCDEFabcdef"), nCharacter) != NULL;
577        }
578        static BOOL IsHexadecimal(const TCHAR* pszCharacters, SIZE_T nCount)
579        {
580                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
581                        if(!IsHexadecimal(pszCharacters[nIndex]))
582                                return FALSE;
583                return TRUE;
584        }
585        static BOOL IsHexadecimal(const TCHAR* pszCharacters)
586        {
587                return IsHexadecimal(pszCharacters, _tcslen(pszCharacters));
588        }
589        BOOL Process(LPCTSTR pszText)
590        {
591                #pragma region Parse
592                if(_tcslen(pszText) > 24)
593                        return FALSE;
594                CString sText = pszText;
595                sText.Trim();
596                #pragma region Delete Trailing L (as in 0x00000000L)
597                if(!sText.IsEmpty() && _tcschr(_T("Ll"), sText[sText.GetLength() - 1]))
598                        sText.Delete(sText.GetLength() - 1);
599                #pragma endregion
600                #pragma region Unquote Single Quote
601                if(sText.GetLength() > 2 && sText[0] == _T('\'') && sText[sText.GetLength() - 1] == _T('\''))
602                {
603                        sText = sText.Mid(1, sText.GetLength() - 2);
604                        sText.Trim();
605                }
606                #pragma endregion
607                if(sText.IsEmpty())
608                        return FALSE;
609                LONGLONG nLongLongResult;
610                #pragma region Hexadecimal
611                if(_tcsnicmp(sText, _T("0x"), 2) == 0)
612                {
613                        if(!IsHexadecimal((LPCTSTR) sText + 2))
614                                return FALSE;
615                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
616                                return FALSE; 
617                } else
618                #pragma endregion
619                #pragma region Integer
620                {
621                        SIZE_T nIndex = 0;
622                        if(sText[0] == _T('-'))
623                                nIndex++;
624                        if(!IsDecimal((LPCTSTR) sText + nIndex))
625                        {
626                                #pragma region Eight Characater Long Hexadecimal without Prefix
627                                if(sText.GetLength() == nIndex + 8 && IsHexadecimal((LPCTSTR) sText + nIndex))
628                                {
629                                        sText.Insert(0, _T("0x"));
630                                        if(!StrToInt64Ex(sText, STIF_SUPPORT_HEX, &nLongLongResult))
631                                                return FALSE; 
632                                } else
633                                #pragma endregion
634                                        return FALSE;
635                        } else
636                                nLongLongResult = _ttoi64((LPCTSTR) sText + nIndex);
637                        if(nIndex)
638                                nLongLongResult = -nLongLongResult;
639                }
640                #pragma endregion
641                const LONG nHighLongLongResult = (LONG) (nLongLongResult >> 32);
642                if(!nLongLongResult || nHighLongLongResult > 0 || nHighLongLongResult < -1)
643                        return FALSE;
644                HRESULT nResult = (LONG) nLongLongResult;
645                #pragma endregion
646                #pragma region Lookup
647                // NOTE: Include file regular expression replacement: ^.+?\#define +([^ ]+?) +MAKE_D3D.+\r?$ -> A($1)
648                CString sTitle, sMessage, sIdentifier;
649                if(IsQuartzResult(nResult, &sMessage))// || LookupQuartzIdentifier(nResult, sIdentifier))
650                {
651                        LookupQuartzIdentifier(nResult, sIdentifier);
652                        sTitle = _T("DirectShow");
653                } else if(IsWmResult(nResult, &sMessage))
654                {
655                        LookupWmIdentifier(nResult, sIdentifier);
656                        sTitle = _T("Windows Media");
657                } else if(IsMfResult(nResult, &sMessage))
658                {
659                        LookupMfIdentifier(nResult, sIdentifier);
660                        sTitle = _T("Media Foundation");
661                } else if(IsAudioClientResult(nResult, &sMessage, &sIdentifier) || LookupAudioClientIdentifier(nResult, sIdentifier))
662                        sTitle = _T("Audio Client");
663                #pragma region Obsolete?
664                // NOTE: These are perhaps useless in Windows 7, but I am under impression they are helpful in earlier systems
665                else if(IsWs2Result(nResult, &sMessage))
666                        sTitle = _T("Sockets");
667                else if(IsWinHttpResult(nResult, &sMessage))
668                        sTitle = _T("WinHTTP");
669                else if(IsWinInetResult(nResult, &sMessage))
670                        sTitle = _T("WinInet");
671                #pragma endregion
672                #pragma region Win32 Priority
673                else if(HRESULT_SEVERITY(nResult) == SEVERITY_ERROR && HRESULT_FACILITY(nResult) == FACILITY_WIN32 && (UINT) HRESULT_CODE(nResult) < 300)
674                {
675                        sMessage = AtlFormatSystemMessage(nResult);
676                        if(!LookupSystemIdentifier(nResult, sIdentifier))
677                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
678                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
679                                sTitle = _T("System");
680                }
681                #pragma endregion
682                else if(IsD3dResult(nResult, &sMessage, &sIdentifier) || LookupD3dIdentifier(nResult, sIdentifier))
683                        sTitle = _T("DirectDraw/Direct3D");
684                else if(IsD2dResult(nResult, &sMessage, &sIdentifier) || LookupD2dIdentifier(nResult, sIdentifier))
685                        sTitle = _T("Direct2D");
686                else if(IsWicResult(nResult, &sMessage, &sIdentifier) || LookupWicIdentifier(nResult, sIdentifier))
687                        sTitle = _T("WinCodec");
688                else if(IsWiaResult(nResult, &sMessage, &sIdentifier) || LookupWiaIdentifier(nResult, sIdentifier))
689                        sTitle = _T("WIA");
690                else if(IsUrtResult(nResult, &sMessage, &sIdentifier) || LookupUrtIdentifier(nResult, sIdentifier))
691                        sTitle = _T(".NET");
692                else if(IsP2pResult(nResult, &sMessage, &sIdentifier) || LookupP2pIdentifier(nResult, sIdentifier))
693                        sTitle = _T("P2P");
694                else if(IsSlResult(nResult, &sMessage, &sIdentifier) || LookupSlIdentifier(nResult, sIdentifier))
695                        sTitle = _T("Software Licensing");
696                else if(IsAviResult(nResult, &sMessage, &sIdentifier) || LookupAviIdentifier(nResult, sIdentifier))
697                        sTitle = _T("AVI");
698                else 
699                {
700                        sMessage = AtlFormatSystemMessage(nResult);
701                        if(!LookupSystemIdentifier(nResult, sIdentifier))
702                                 LookupHresultSystemIdentifier(nResult, sIdentifier);
703                        if(!sMessage.IsEmpty() || !sIdentifier.IsEmpty())
704                                sTitle = _T("System");
705                }
706                if(sMessage.IsEmpty() && sTitle.IsEmpty())
707                        return FALSE;
708                #pragma endregion
709                #pragma region Present
710                _A(!sTitle.IsEmpty());
711                m_sQuery = AtlFormatString(_T("0x%08X"), nResult) + _T(" ") + sIdentifier + _T(" ") + sMessage;
712                CRoArrayT<CString> TitleArray;
713                _W(TitleArray.Add(AtlFormatString(_T("0x%08X"), nResult)) >= 0);
714                if(!sIdentifier.IsEmpty())
715                        _W(TitleArray.Add(sIdentifier) >= 0);
716                if(!sTitle.IsEmpty())
717                        _W(TitleArray.Add(sTitle) >= 0);
718                if(sMessage.IsEmpty())
719                        sMessage = _T("(no message found)");
720                SetBalloonToolTip(NIIF_INFO, _StringHelper::Join(TitleArray, _T(" - ")), sMessage);
721                #pragma endregion
722                return TRUE;
723        }
724
725// Window message handelrs
726        LRESULT OnCreate(CREATESTRUCT*)
727        {
728                m_nCommonControlsVersion = GetCommonControlsVersion();
729                m_nShellVersion = GetShellVersion();
730                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
731                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
732                CIcon Icon = AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
733                CString sTip = AtlLoadString(IDS_PROJNAME);
734                ZeroMemory(&m_NotifyIconData, sizeof m_NotifyIconData);
735                m_NotifyIconData.hWnd = m_hWnd;
736                m_NotifyIconData.uID = 1;
737                m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_STATE;
738                m_NotifyIconData.uCallbackMessage = WM_NOTIFYICON;
739                m_NotifyIconData.hIcon = Icon;
740                _tcsncpy_s(m_NotifyIconData.szTip, sTip,  _TRUNCATE);
741                if(m_nShellVersion >= 0x0005000000000000) // 5.0
742                {
743                        m_NotifyIconData.cbSize = sizeof m_NotifyIconData; 
744                        m_NotifyIconData.uFlags |= NIF_INFO;
745                        _tcsncpy_s(m_NotifyIconData.szInfoTitle, GetDefaultInfoTitle(), _TRUNCATE);
746                        m_NotifyIconData.dwInfoFlags = GetDefaultInfoFlags();
747                } else
748                        m_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE; 
749                _W(Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData));
750                m_NotifyIconData.uFlags &= ~(NIF_ICON | NIF_STATE);
751                m_NextClipboardViewerWindow = SetClipboardViewer();
752                _pAtlModule->Lock();
753                #if defined(_DEBUG)
754                //Process(AtlFormatString(_T("%d"), 0x80040227)); // VFW_E_WRONG_STATE
755                //Process(_T("0xC00D36B9")); // MF_E_NO_MORE_TYPES)
756                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(WSAEADDRINUSE))); // WSAEADDRINUSE
757                //Process(AtlFormatString(_T("0x%x"), HRESULT_FROM_WIN32(ERROR_WINHTTP_AUTODETECTION_FAILED))); // ERROR_WINHTTP_AUTODETECTION_FAILED
758                Process(_T("0x8004406a"));
759                //Process(_T("-2147312566"));
760                #endif // defined(_DEBUG)
761                return TRUE;
762        }
763        LRESULT OnDestroy()
764        {
765                _pAtlModule->Unlock();
766                _W(ChangeClipboardChain(m_NextClipboardViewerWindow));
767                _W(Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData));
768                m_NotifyIconData.hWnd = NULL;
769                // NOTE: Safety double-posting
770                PostQuitMessage(0);
771                return 0;
772        }
773        LRESULT OnChangeCbChain(CWindow RemovedWindow, CWindow NextWindow)
774        {
775                if(m_NextClipboardViewerWindow == RemovedWindow)
776                        m_NextClipboardViewerWindow = NextWindow;
777                else if(m_NextClipboardViewerWindow)
778                        m_NextClipboardViewerWindow.SendMessage(WM_CHANGECBCHAIN, (WPARAM) (HWND) RemovedWindow, (LPARAM) (HWND) NextWindow);
779                return 0;
780        }
781        LRESULT OnDrawClipboard()
782        {
783                if(OpenClipboard()) 
784                {
785                        _ATLTRY
786                        {
787                                if(IsClipboardFormatAvailable(CF_UNICODETEXT))
788                                {
789                                        CGlobalMemoryWeakHandle DataHandle = (HGLOBAL) GetClipboardData(CF_UNICODETEXT);
790                                        __E(DataHandle);
791                                        CGlobalMemoryHandle::CLockT<WCHAR> pszData(DataHandle);
792                                        Process(CW2CT(pszData));
793                                }
794                        }
795                        _ATLCATCHALL()
796                        {
797                                _W(CloseClipboard());
798                                _ATLRETHROW;
799                        }
800                        _W(CloseClipboard());
801                }
802                if(m_NextClipboardViewerWindow)
803                        m_NextClipboardViewerWindow.SendMessage(WM_DRAWCLIPBOARD, m_pCurrentMsg->wParam, m_pCurrentMsg->lParam);
804                return 0;
805        }
806        LRESULT OnMouseMove(UINT, CPoint)
807        {
808                return 0;
809        }
810        LRESULT OnLButtonDblClk(UINT, CPoint)
811        {
812                if(!IsWindowEnabled())
813                        return 0;
814                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
815                //CWaitCursor WaitCursor;
816                if(nDefaultCommandIdentifier)
817                        _W(PostMessage(WM_COMMAND, nDefaultCommandIdentifier));
818                return 0;
819        }
820        LRESULT OnRButtonUp(UINT, CPoint)
821        {
822                CMenu ContainerMenu;
823                _W(ContainerMenu.LoadMenu(IDR_NOTIFYICON));
824                CMenuHandle Menu;
825                INT nDefaultCommandIdentifier = ID_APP_ABOUT;
826                Menu = ContainerMenu.GetSubMenu(0);
827                //CWaitCursor WaitCursor;
828                _A(Menu);
829                if(nDefaultCommandIdentifier)
830                        _W(Menu.SetMenuDefaultItem(nDefaultCommandIdentifier));
831                CPoint Position;
832                GetCursorPos(&Position);
833                _W(SetForegroundWindow(m_hWnd));
834                _W(Menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERTICAL, Position.x, Position.y, m_hWnd));
835                return 0;
836        }
837        LRESULT OnNotifyIconBalloonUserClick(UINT, WPARAM, LPARAM)
838        {
839                if(m_sQuery.IsEmpty())
840                        return 0;
841                CWinHttpRequestIdentifier Identifier(_T("http://google.com/search"));
842                Identifier.AddSearchValue(_T("q"), m_sQuery);
843                CWaitCursor WaitCursor;
844                ShellExecute(m_hWnd, NULL, CW2CT(Identifier.GetValue()), NULL, NULL, SW_SHOWDEFAULT);
845                return 0;
846        }
847        LRESULT OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam)
848        {
849                _A(wParam == m_NotifyIconData.uID);
850                return SendMessage((UINT) lParam);
851        }
852        LRESULT OnApplicationAbout(UINT, INT, HWND)
853        {
854                CAboutDialog Dialog;
855                EnableWindow(FALSE);
856                Dialog.DoModal();
857                EnableWindow(TRUE);
858                return 0;
859        }
860        LRESULT OnApplicationExit(UINT = 0, INT = ID_APP_EXIT, HWND = NULL)
861        {
862                PostQuitMessage(0);
863                return 0;
864        }
865};
Note: See TracBrowser for help on using the repository browser.