Created
October 12, 2017 14:26
-
-
Save ysc3839/25e8ed113c4e975b6781c9759ed4ee87 to your computer and use it in GitHub Desktop.
Undocumented API ITrayNotify.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tchar.h> | |
#include <windows.h> | |
// The known values for NOTIFYITEM's dwPreference member. | |
enum NOTIFYITEM_PREFERENCE | |
{ | |
// In Windows UI: "Only show notifications." | |
PREFERENCE_SHOW_WHEN_ACTIVE = 0, | |
// In Windows UI: "Hide icon and notifications." | |
PREFERENCE_SHOW_NEVER = 1, | |
// In Windows UI: "Show icon and notifications." | |
PREFERENCE_SHOW_ALWAYS = 2 | |
}; | |
typedef struct tagNOTIFYITEM | |
{ | |
PWSTR pszExeName; | |
PWSTR pszTip; | |
HICON hIcon; | |
HWND hWnd; | |
NOTIFYITEM_PREFERENCE dwPreference; | |
UINT uID; | |
GUID guidItem; | |
} NOTIFYITEM, *PNOTIFYITEM; | |
MIDL_INTERFACE("D782CCBA-AFB0-43F1-94DB-FDA3779EACCB") INotificationCB : public IUnknown | |
{ | |
public: | |
BEGIN_INTERFACE | |
virtual HRESULT STDMETHODCALLTYPE Notify(ULONG, NOTIFYITEM *) = 0; | |
END_INTERFACE | |
}; | |
MIDL_INTERFACE("FB852B2C-6BAD-4605-9551-F15F87830935") ITrayNotify : public IUnknown | |
{ | |
public: | |
BEGIN_INTERFACE | |
virtual HRESULT STDMETHODCALLTYPE RegisterCallback(INotificationCB* callback) = 0; | |
virtual HRESULT STDMETHODCALLTYPE SetPreference(const NOTIFYITEM* notify_item) = 0; | |
virtual HRESULT STDMETHODCALLTYPE EnableAutoTray(BOOL enabled) = 0; | |
END_INTERFACE | |
}; | |
MIDL_INTERFACE("D133CE13-3537-48BA-93A7-AFCD5D2053B4") ITrayNotifyWin8 : public IUnknown | |
{ | |
public: | |
BEGIN_INTERFACE | |
virtual HRESULT STDMETHODCALLTYPE RegisterCallback(INotificationCB* callback, ULONG*) = 0; | |
virtual HRESULT STDMETHODCALLTYPE UnregisterCallback(ULONG*) = 0; | |
virtual HRESULT STDMETHODCALLTYPE SetPreference(NOTIFYITEM const*) = 0; | |
virtual HRESULT STDMETHODCALLTYPE EnableAutoTray(BOOL) = 0; | |
virtual HRESULT STDMETHODCALLTYPE DoAction(BOOL) = 0; | |
END_INTERFACE | |
}; | |
const CLSID CLSID_TrayNotify = { 0x25DEAD04, 0x1EAC, 0x4911,{ 0x9E, 0x3A, 0xAD, 0x0A, 0x4A, 0xB5, 0x60, 0xFD } }; | |
template <class T> | |
class NotificationMgr : public INotificationCB | |
{ | |
public: | |
NotificationMgr(T *pTrayNotify) : m_pTrayNotify(pTrayNotify) {} | |
IFACEMETHODIMP QueryInterface(REFIID riid, PVOID *ppv) | |
{ | |
if (ppv == nullptr) | |
return E_POINTER; | |
if (riid == IID_IUnknown) | |
*ppv = static_cast<IUnknown *>(this); | |
else if (riid == __uuidof (INotificationCB)) | |
*ppv = static_cast<INotificationCB *>(this); | |
else | |
return E_NOINTERFACE; | |
reinterpret_cast<IUnknown*>(*ppv)->AddRef(); | |
return S_OK; | |
} | |
IFACEMETHODIMP_(ULONG) AddRef() { return 1; } | |
IFACEMETHODIMP_(ULONG) Release() { return 1; } | |
IFACEMETHODIMP Notify(ULONG Event, NOTIFYITEM *NotifyItem) | |
{ | |
wchar_t *copy = _wcsdup(NotifyItem->pszExeName); | |
if (copy) | |
{ | |
_wcslwr_s(copy, wcslen(NotifyItem->pszExeName) + 1); | |
if (wcsstr(copy, L"telegram.exe")) | |
{ | |
if (m_pTrayNotify) | |
{ | |
NotifyItem->dwPreference = PREFERENCE_SHOW_ALWAYS; | |
m_pTrayNotify->SetPreference(NotifyItem); | |
} | |
} | |
free(copy); | |
} | |
return S_OK; | |
} | |
private: | |
T *m_pTrayNotify; | |
}; | |
int main() | |
{ | |
CoInitialize(nullptr); | |
IUnknown *pIUnk; | |
HRESULT hr = CoCreateInstance(CLSID_TrayNotify, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&pIUnk)); | |
if (SUCCEEDED(hr)) | |
{ | |
ITrayNotifyWin8 *pTrayNotifyW8; | |
hr = pIUnk->QueryInterface(&pTrayNotifyW8); | |
if (SUCCEEDED(hr)) | |
{ | |
ULONG callback_id = 0; | |
NotificationMgr<ITrayNotifyWin8> NotiMgr8(pTrayNotifyW8); | |
hr = pTrayNotifyW8->RegisterCallback(&NotiMgr8, &callback_id); | |
if (SUCCEEDED(hr)) | |
hr = pTrayNotifyW8->UnregisterCallback(&callback_id); | |
pTrayNotifyW8->Release(); | |
} | |
else | |
{ | |
ITrayNotify *pTrayNotify; | |
hr = pIUnk->QueryInterface(&pTrayNotify); | |
if (SUCCEEDED(hr)) | |
{ | |
NotificationMgr<ITrayNotify> NotiMgr(pTrayNotify); | |
hr = pTrayNotify->RegisterCallback(&NotiMgr); | |
if (SUCCEEDED(hr)) | |
pTrayNotify->RegisterCallback(nullptr); | |
pTrayNotify->Release(); | |
} | |
} | |
pIUnk->Release(); | |
} | |
return 0; | |
} |
C++写出C#风格
WinAPI COM模板编程
第一次见,好...好厉害....
no effect on windows 10 Version 1709 (OS Build 16299.125)
could you pls update it to work with win10 1803 (17134.1)?
i used a similar app (below link) but it stopped working since version 1709 in december
https://hianz.wordpress.com/2013/09/03/new-windows-tray-notification-manager-is-here/
typedef struct tagNOTIFYITEM {
PWSTR lpszExeName; <---- Causing crash
PWSTR lpszTip; <---- or this Causing crash
HICON hIcon;
HWND hWnd;
NOTIFYITEM_PREFERENCE dwPreference;
UINT uID;
GUID guidItem;
} NOTIFYITEM;
This structure causing fatal crash in vb6 on Win11 Home Edition.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Windows 10 x64 15063.674.
References:
https://chromium.googlesource.com/chromium/src.git/+/master/chrome/browser/ui/views/status_icons/
https://bbs.pediy.com/thread-202658.htm