Skip to content

Commit

Permalink
fix traynotify crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktorwiktor12 committed Jan 30, 2025
1 parent 9b43f1a commit 8808ae5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
11 changes: 6 additions & 5 deletions interfacesp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ INotificationCB : IUnknown
MIDL_INTERFACE("D133CE13-3537-48BA-93A7-AFCD5D2053B4")
ITrayNotify : IUnknown
{
STDMETHOD(RegisterCallback)(INotificationCB * callback, ULONG*) PURE;
STDMETHOD(UnregisterCallback)(ULONG*) PURE;
STDMETHOD(SetPreference)(LPNOTIFYITEM) PURE;
STDMETHOD(EnableAutoTray)(BOOL) PURE;
STDMETHOD(DoAction)(BOOL) PURE;
virtual STDMETHOD(RegisterCallback)(INotificationCB * callback, ULONG*) PURE;
virtual STDMETHOD(UnregisterCallback)(ULONG*) PURE;
virtual STDMETHOD(SetPreference)(NOTIFYITEM) PURE;
virtual STDMETHOD(EnableAutoTray)(BOOL) PURE;
virtual STDMETHOD(DoAction)(BOOL) PURE;
virtual STDMETHOD(SetWindowingEnvironmentConfig)(IUnknown*) PURE;
};

MIDL_INTERFACE("2F711B17-773C-41D4-93FA-7F23EDCECB66")
Expand Down
2 changes: 2 additions & 0 deletions shundoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ typedef struct _TRAYAPPBARDATA
DWORD dwProcId;
} TRAYAPPBARDATA, * PTRAYAPPBARDATA;

#pragma pack(push,0x1)
typedef struct tagNOTIFYITEM
{
LPWSTR pszExeName;
Expand All @@ -226,6 +227,7 @@ typedef struct tagNOTIFYITEM
UINT uID;
GUID guidItem;
} NOTIFYITEM, * LPNOTIFYITEM;
#pragma pack(pop)

typedef struct _NOTIFYICONDATA32A {
DWORD cbSize;
Expand Down
49 changes: 15 additions & 34 deletions traynot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,12 @@ int CALLBACK DeleteDPAPtrCB(TNINFOITEM *pItem, void *pData)
return TRUE;
}

//
// Stub for CTrayNotify, so as to not break the COM rules of refcounting a static object
//
class ATL_NO_VTABLE CTrayNotifyStub :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CTrayNotifyStub, &CLSID_TrayNotify>,
public ITrayNotify
{
public:
CTrayNotifyStub() {};
virtual ~CTrayNotifyStub() {};

DECLARE_NOT_AGGREGATABLE(CTrayNotifyStub)

BEGIN_COM_MAP(CTrayNotifyStub)
COM_INTERFACE_ENTRY(ITrayNotify)
END_COM_MAP()

// *** ITrayNotify method ***
STDMETHODIMP RegisterCallback(INotificationCB* pNotifyCB, ULONG*);
STDMETHODIMP UnregisterCallback(ULONG*);
STDMETHODIMP SetPreference(LPNOTIFYITEM pNotifyItem);
STDMETHODIMP EnableAutoTray(BOOL bTraySetting);
STDMETHODIMP DoAction(BOOL bTraySetting);
};

//
// CTrayNotifyStub functions...
//
HRESULT CTrayNotifyStub::SetPreference(LPNOTIFYITEM pNotifyItem)
HRESULT CTrayNotifyStub::SetPreference(NOTIFYITEM pNotifyItem)
{
return c_tray._trayNotify.SetPreference(pNotifyItem);
}
Expand All @@ -129,6 +105,11 @@ HRESULT CTrayNotifyStub::DoAction(BOOL bTraySetting)
return S_OK;
}

STDMETHODIMP_(HRESULT __stdcall) CTrayNotifyStub::SetWindowingEnvironmentConfig(IUnknown* unk)
{
return E_NOTIMPL;
}

HRESULT CTrayNotifyStub_CreateInstance(IUnknown* pUnkOuter, IUnknown** ppunk)
{
if (pUnkOuter != NULL)
Expand Down Expand Up @@ -274,28 +255,28 @@ HRESULT CTrayNotify::RegisterCallback(INotificationCB* pNotifyCB)
return S_OK;
}

HRESULT CTrayNotify::SetPreference(LPNOTIFYITEM pNotifyItem)
HRESULT CTrayNotify::SetPreference(NOTIFYITEM pNotifyItem)
{
// This function should NEVER be called if the NoTrayItemsDisplayPolicy is enabled...
ASSERT(!_fNoTrayItemsDisplayPolicyEnabled);

ASSERT(!GetIsNoAutoTrayPolicyEnabled());

ASSERT( pNotifyItem->dwUserPref == TNUP_AUTOMATIC ||
pNotifyItem->dwUserPref == TNUP_DEMOTED ||
pNotifyItem->dwUserPref == TNUP_PROMOTED );
ASSERT(pNotifyItem.dwUserPref == TNUP_AUTOMATIC ||
pNotifyItem.dwUserPref == TNUP_DEMOTED ||
pNotifyItem.dwUserPref == TNUP_PROMOTED);

INT_PTR iItem = -1;

if (pNotifyItem->hWnd)
if (pNotifyItem.hWnd)
{
iItem = m_TrayItemManager.FindItemAssociatedWithHwndUid(pNotifyItem->hWnd, pNotifyItem->uID);
iItem = m_TrayItemManager.FindItemAssociatedWithHwndUid(pNotifyItem.hWnd, pNotifyItem.uID);
if (iItem != -1)
{
CTrayItem * pti = m_TrayItemManager.GetItemDataByIndex(iItem);
if (pti && pti->dwUserPref != pNotifyItem->dwUserPref)
if (pti && pti->dwUserPref != pNotifyItem.dwUserPref)
{
pti->dwUserPref = pNotifyItem->dwUserPref;
pti->dwUserPref = pNotifyItem.dwUserPref;
// If the preference changes, the accumulated time must start again...
if (pti->IsStartupIcon())
pti->uNumSeconds = 0;
Expand All @@ -309,7 +290,7 @@ HRESULT CTrayNotify::SetPreference(LPNOTIFYITEM pNotifyItem)
}
else
{
if (m_TrayItemRegistry.SetPastItemPreference(pNotifyItem))
if (m_TrayItemRegistry.SetPastItemPreference(&pNotifyItem))
return S_OK;
}

Expand Down
29 changes: 28 additions & 1 deletion traynot.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class CTrayNotify : public CImpWndProc
STDMETHODIMP_(ULONG) Release();

// *** ITrayNotify methods, which are called from the CTrayNotifyStub ***
STDMETHODIMP SetPreference(LPNOTIFYITEM pNotifyItem);
STDMETHODIMP SetPreference(NOTIFYITEM pNotifyItem);
STDMETHODIMP RegisterCallback(INotificationCB* pNotifyCB);
STDMETHODIMP EnableAutoTray(BOOL bTraySetting);

Expand Down Expand Up @@ -342,4 +342,31 @@ class CTrayNotify : public CImpWndProc
BALLOONEVENT _beLastBalloonEvent;
};

//
// Stub for CTrayNotify, so as to not break the COM rules of refcounting a static object
//
class CTrayNotifyStub :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CTrayNotifyStub, &CLSID_TrayNotify>,
public ITrayNotify
{
public:
CTrayNotifyStub() {};
virtual ~CTrayNotifyStub() {};

//DECLARE_NOT_AGGREGATABLE(CTrayNotifyStub)

BEGIN_COM_MAP(CTrayNotifyStub)
COM_INTERFACE_ENTRY(ITrayNotify)
END_COM_MAP()

// *** ITrayNotify method ***
virtual STDMETHODIMP RegisterCallback(INotificationCB* pNotifyCB, ULONG*) override;
virtual STDMETHODIMP UnregisterCallback(ULONG*) override;
virtual STDMETHODIMP SetPreference(NOTIFYITEM pNotifyItem) override;
virtual STDMETHODIMP EnableAutoTray(BOOL bTraySetting) override;
virtual STDMETHODIMP DoAction(BOOL bTraySetting) override;
virtual STDMETHODIMP SetWindowingEnvironmentConfig(IUnknown* unk) override;
};

#endif // _TRAYNOT_H
6 changes: 3 additions & 3 deletions trayprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class ATL_NO_VTABLE CNotificationsDlg :

CSimpleArray<CNotificationItem> _saItems; //copy of the data, initialized by user
BOOL _fItemChanged;
ITrayNotify* _pTrayNotify;
CTrayNotifyStub* _pTrayNotify;
int _nPrevIndex;
HWND _hwndCombo;
HWND _hwndListView;
Expand Down Expand Up @@ -519,7 +519,7 @@ LRESULT CNotificationsDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam,
}

// localserver for tray notify
if (SUCCEEDED(CoCreateInstanceHook(CLSID_TrayNotify, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&_pTrayNotify))))
if (SUCCEEDED(CoCreateInstanceHook(CLSID_TrayNotify, NULL, CLSCTX_LOCAL_SERVER, __uuidof(ITrayNotify), (void**)&_pTrayNotify)))
{
INotificationCB* pCB = 0;

Expand Down Expand Up @@ -735,7 +735,7 @@ void CNotificationsDlg::ApplyChanges(void)
{
for (int i = 0; i < _saItems.GetSize(); i++)
{
_pTrayNotify->SetPreference(&_saItems[i]);
_pTrayNotify->SetPreference(_saItems[i]);
}
}
}
Expand Down

0 comments on commit 8808ae5

Please sign in to comment.