/* Compile with: cl /EHsc traynotify.cpp */ #include #include #include #include const GUID CLSID_TrayNotify = { 0x25DEAD04, 0x1EAC, 0x4911, 0x9E, 0x3A, 0xAD, 0x0A, 0x4A, 0xB5, 0x60, 0xFD }; const GUID IID_ITrayNotify = { 0xFB852B2C, 0x6BAD, 0x4605, 0x95, 0x51, 0xF1, 0x5F, 0x87, 0x83, 0x09, 0x35 }; const GUID IID_INotificationCB = { 0xD782CCBA, 0xAFB0, 0x43F1, 0x94, 0xDB, 0xFD, 0xA3, 0x77, 0x9E, 0xAC, 0xCB }; #define NOTIFY_PREFERENCE_HIDE_INACTIVE 0 #define NOTIFY_PREFERENCE_ALWAYS_HIDE 1 #define NOTIFY_PREFERENCE_ALWAYS_SHOW 2 typedef struct tagNOTIFYITEM { PWSTR pszExeName; PWSTR pszTip; HICON hIcon; HWND hWnd; DWORD dwPreference; UINT uID; GUID guidItem; } NOTIFYITEM; MIDL_INTERFACE("D782CCBA-AFB0-43F1-94DB-FDA3779EACCB") INotificationCB : public IUnknown { public: virtual HRESULT STDMETHODCALLTYPE Notify (ULONG, NOTIFYITEM *) = 0; }; MIDL_INTERFACE("FB852B2C-6BAD-4605-9551-F15F87830935") ITrayNotify : public IUnknown { public: virtual HRESULT STDMETHODCALLTYPE RegisterCallback (INotificationCB *) = 0; virtual HRESULT STDMETHODCALLTYPE SetPreference (NOTIFYITEM const *) = 0; virtual HRESULT STDMETHODCALLTYPE EnableAutoTray (BOOL) = 0; }; class NotificationCB : public INotificationCB { public: NotificationCB() { ; } virtual ~NotificationCB() { ; } ULONG STDMETHODCALLTYPE AddRef() { return 2; } ULONG STDMETHODCALLTYPE Release() { return 1; } HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppv) { static QITAB rgqit[] = { QITABENT(NotificationCB, INotificationCB), { 0 }, }; return QISearch(this, rgqit, riid, ppv); } HRESULT STDMETHODCALLTYPE Notify(ULONG value, NOTIFYITEM *item) { std::cout << "Notify entry" << std::endl; std::cout << "> value: " << value << std::endl; std::wcout << L"> pszExeName: " << item->pszExeName << std::endl; std::wcout << L"> pszTip: " << item->pszTip << std::endl; std::cout << "> hIcon: " << item->hIcon << std::endl; std::cout << "> hWnd: " << item->hWnd << std::endl; std::cout << "> dwPreference: " << item->dwPreference << std::endl; std::cout << "> uID: " << item->uID << std::endl; CComHeapPtr bstrGuid; StringFromCLSID(item->guidItem, &bstrGuid); std::wcout << L"> guidItem: " << bstrGuid << std::endl; std::cout << std::endl; return S_OK; } }; #ifndef IID_PPV_ARG #define IID_PPV_ARG(Itype, ppType) IID_##Itype, reinterpret_cast((static_cast(ppType))) #endif void run() { CComPtr trayNotify; HRESULT hr = CoCreateInstance(CLSID_TrayNotify, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARG(ITrayNotify, &trayNotify)); if (SUCCEEDED(hr)) { NotificationCB cb; trayNotify->RegisterCallback(&cb); trayNotify->RegisterCallback(NULL); } else { std::cout << "Could not create ITrayNotify: " << std::hex << hr << std::endl; } } int main() { CoInitialize(NULL); run(); CoUninitialize(); return 0; }