Fix: tray icon not showing in Release service+agent mode (drop NIF_GUID)

NIF_GUID binds the tray icon to the EXE's full path. Once a GUID is
registered for one path, Shell_NotifyIcon(NIM_ADD) silently fails for any
other path using the same GUID. This caused Debug-vs-Release builds and
service+agent dual-process scenarios to fight over the same GUID slot.

Drop NIF_GUID and the static NOTIFY_ICON_GUID; revert to the traditional
(hWnd, uID) identification. The icon is freshly registered per process
launch, no path binding, no cross-instance interference.

The NIF_GUID was leftover from the AUMID/Toast experiment that was later
reverted; only the tray-icon side of that change wasn't cleaned up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
yuanyuanxiang
2026-05-07 00:17:32 +02:00
parent ef8165c3b4
commit 5f4fb62d20

View File

@@ -1144,19 +1144,18 @@ VOID CMy2015RemoteDlg::CreatStatusBar()
VOID CMy2015RemoteDlg::CreateNotifyBar()
{
// GUID 用于 Windows 10/11 Toast 通知关联托盘图标
// {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}
static const GUID NOTIFY_ICON_GUID =
{ 0xA1B2C3D4, 0xE5F6, 0x7890, { 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90 } };
m_Nid.uVersion = NOTIFYICON_VERSION_4;
m_Nid.cbSize = sizeof(NOTIFYICONDATA); //大小赋值
m_Nid.hWnd = m_hWnd; //父窗口 是被定义在父类CWnd类中
m_Nid.uID = IDR_MAINFRAME; //icon ID
m_Nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_GUID; //托盘所拥有的状态
// 注意:不加 NIF_GUID。NIF_GUID 会把托盘图标的注册和 EXE 完整路径绑死
// MSDNIf a Shell_NotifyIcon call uses a GUID that is recognized as
// belonging to a different application path, the call will fail
// 导致 Debug 和 Release 编译产物(路径不同)相互冲突——先注册的占住 GUID
// 后启动的 NIM_ADD 静默失败、托盘没图标。
m_Nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; //托盘所拥有的状态
m_Nid.uCallbackMessage = UM_ICONNOTIFY; //回调消息
m_Nid.hIcon = m_hIcon; //icon 变量
m_Nid.guidItem = NOTIFY_ICON_GUID;
// 先删除可能残留的旧图标(程序异常退出时可能残留)
Shell_NotifyIcon(NIM_DELETE, &m_Nid);