Skip to content

Instantly share code, notes, and snippets.

@ysc3839
Created October 12, 2017 14:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ysc3839/25e8ed113c4e975b6781c9759ed4ee87 to your computer and use it in GitHub Desktop.
Save ysc3839/25e8ed113c4e975b6781c9759ed4ee87 to your computer and use it in GitHub Desktop.
Undocumented API ITrayNotify.
#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;
}
@ysc3839
Copy link
Author

ysc3839 commented Oct 12, 2017

@Lyoko-Jeremie
Copy link

C++写出C#风格
WinAPI COM模板编程

第一次见,好...好厉害....

@zhengran14
Copy link

no effect on windows 10 Version 1709 (OS Build 16299.125)

@sign-in-smth
Copy link

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment